Reading text file
Afficher commentaires plus anciens
Hi!
This seems like an easy one but I can never wrap my head around reading text files.
I have a text file which contains u and v components of wind and pressure for a cyclone at a certain grid point. Here's a snipet of the data...
GridID u wind v wind Pressure
159838 5.25 -1.6 1018.8
159839 5.19 -2.1 1018.8
159840 5.03 -2.5 1018.8
159841 4.76 -3.0 1018.8
159842 4.42 -3.3 1018.8
#
160945 5.13 -5.3 1018.5
160946 4.38 -5.4 1018.6
160947 3.66 -5.5 1018.7
160948 3.00 -5.3 1018.8
160949 2.42 -5.1 1018.8
160950 1.91 -4.8 1018.9
etc...
The # means that the following data is on another time step. Each timestep is a different size as the cyclone does not affect every grid cell. I want to load this data into a matrix.
Any suggestions on the best way to do this? Any help would be greatly appreciated :)
Réponse acceptée
Plus de réponses (1)
Walter Roberson
le 3 Oct 2011
We need more information about how you want this stored. You say you want the data stored in a matrix, but do you want all of the data stored in a single matrix, ignoring the '#' boundaries, or do you want one matrix (or at least one cell) for each section ?
If you want everything in one matrix ignoring the '#', then
fid = fopen('YourFile.txt', 'rt');
datacell = textscan('%f%f%f%f', 'HeaderLines', 1, 'CombineOutput', 1, 'CommentStyle', '#');
fclose(fid);
data = datacell{1};
1 commentaire
Leigh
le 3 Oct 2011
Catégories
En savoir plus sur Characters and Strings dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!