MATLAB Reading Text File with Latitude, Longitude & Station Name

AI Thread Summary
The discussion focuses on reading data from text files in MATLAB, specifically a file containing latitude, longitude, and station names. The user initially struggles with using `fscanf` to read the entire file but finds success with `textread`, which allows for reading the data into separate variables. A suggestion is made to use the `load` function for files with a consistent format, as it simplifies the process by loading the data into a structured array. Additionally, the user inquires about reading another text file that includes slashes in its data format (e.g., dates), and it is recommended to adjust the format string in `textread` to accommodate this new structure. Overall, the discussion emphasizes efficient methods for reading mixed data types from text files in MATLAB.
Chileah
Messages
2
Reaction score
0
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
 
Physics news on Phys.org
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.
 
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
 
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...
 
Chileah said:
Now I have another text file with / in it (ex.2000/2/5). I'm not sure how to read that. Any ideas?
I would think that just changing the format string would work. ex:
[year month day]=textread('data.txt', '%f/%f/%f')
 

Similar threads

Replies
8
Views
2K
Replies
1
Views
2K
Replies
4
Views
7K
Replies
3
Views
2K
Replies
2
Views
2K
Replies
2
Views
1K
Back
Top