Reading Text File with Latitude, Longitude & Station Name

Click For Summary

Discussion Overview

The discussion revolves around reading text files containing latitude, longitude, and station names in MATLAB. Participants explore various methods for reading data from files, including handling different formats and potential issues encountered during the process.

Discussion Character

  • Technical explanation
  • Homework-related
  • Exploratory

Main Points Raised

  • One participant, Leah, describes her initial attempt to read a text file using fscanf and seeks advice on reading the entire file.
  • Another participant suggests using a loop to read each line of the file until the end is reached, combining the lines into a single string.
  • Leah later shares that she successfully read the file using textread and provides her code as an example.
  • Leah raises a new question about reading a different text file that contains slashes in the data (e.g., 2000/2/5) and seeks guidance on how to handle this format.
  • A participant proposes that changing the format string in textread could work for the new file format, providing an example of how to do so.

Areas of Agreement / Disagreement

Participants present various methods for reading files, but there is no consensus on the best approach, as different solutions are offered for different scenarios. The discussion remains open-ended with multiple competing views on how to handle file reading in MATLAB.

Contextual Notes

Some methods suggested may depend on the specific structure of the text files, and the effectiveness of each approach may vary based on the data format and content.

Who May Find This Useful

This discussion may be useful for MATLAB users dealing with file input/output, particularly those working with text files containing mixed data types.

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 ·
Replies
8
Views
3K
Replies
1
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K