Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab help, fscanf

  1. Nov 13, 2006 #1
    Hi,
    I have a text file with 3 columns (latitude, longitude, & station name) and 51 rows. Using fscanf I am able to read the first row of the file but not the whole file. Any ideas of how to read to the end of a text file that contains both numbers and letters?

    This is what I have:
    fid=fopen('latlon_LAstations.txt');
    %station_lat=fscanf(fid,'%g',[1]);
    %station_lon=fscanf(fid,'%g',[2]);
    %station_name=fscanf(fid,'%s',[1]);

    Thanks for you help,
    Leah
     
  2. jcsd
  3. Nov 13, 2006 #2

    0rthodontist

    User Avatar
    Science Advisor

    Without knowing anything about Matlab, one option is to make a loop that reads each individual line from the file until you reach the end of the file, sticking the lines together into a single string as you go.
     
  4. Nov 14, 2006 #3
    I was able to read the file using textread.

    Here is my code:
    [lat,lon,sta]=textread('latlon_LAstations.txt', '%f %f %s')

    Now I have another text file with / in it (ex.2000/2/5). I'm not sure how to read that. Any ideas?

    Thanks,
    Leah
     
  5. Nov 18, 2006 #4

    Dr Transport

    User Avatar
    Science Advisor
    Gold Member

    if the file is in the same format, i.e. 3 columns and 51 lines, use the load function, it will read the file ito a 3 x 51 array, then break the arrays into the vectors you want, should take all of 5 lines of code to do it. Looping in Matlab is slow.....
     
  6. Nov 18, 2006 #5
    I would think that just changing the format string would work. ex:
    [year month day]=textread('data.txt', '%f/%f/%f')
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook




Loading...
Similar Threads for Matlab help fscanf Date
MATLAB MATLab: Not enough inputs for nlinfit Nov 16, 2017
MATLAB Help needed to fix a matlab code Nov 8, 2017
New to Matlab, help with vectors Feb 20, 2016
Need help with integral Dec 23, 2015
MATLAB 2D diffusion equation, need help for matlab code. Nov 7, 2015