Reading text file

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

Fangjun Jiang
Fangjun Jiang le 3 Oct 2011

0 votes

One way to do it.
clear;
fid=fopen('test.txt');
str=textscan(fid,'%s','headerlines',1);
fclose(fid);
str=str{1};
str=[{'#'};str;{'#'}];
index=find(strcmp(str,'#'));
N=length(index);
Data=cell(N-1,1);
for k=1:N-1
Data{k}=reshape(str2double(str(index(k)+1:index(k+1)-1)),4,[])';
end
>> Data
Data =
[5x4 double]
[6x4 double]
[3x4 double]
>> Data{1}
ans =
1.0e+005 *
1.598380000000000 0.000052500000000 -0.000016000000000 0.010188000000000
1.598390000000000 0.000051900000000 -0.000021000000000 0.010188000000000
1.598400000000000 0.000050300000000 -0.000025000000000 0.010188000000000
1.598410000000000 0.000047600000000 -0.000030000000000 0.010188000000000
1.598420000000000 0.000044200000000 -0.000033000000000 0.010188000000000
>> Data{2}
ans =
1.0e+005 *
1.609450000000000 0.000051300000000 -0.000053000000000 0.010185000000000
1.609460000000000 0.000043800000000 -0.000054000000000 0.010186000000000
1.609470000000000 0.000036600000000 -0.000055000000000 0.010187000000000
1.609480000000000 0.000030000000000 -0.000053000000000 0.010188000000000
1.609490000000000 0.000024200000000 -0.000051000000000 0.010188000000000
1.609500000000000 0.000019100000000 -0.000048000000000 0.010189000000000
>> Data{3}
ans =
1.0e+005 *
1.609450000000000 0.000051300000000 -0.000053000000000 0.010185000000000
1.609460000000000 0.000043800000000 -0.000054000000000 0.010186000000000
1.609470000000000 0.000036600000000 -0.000055000000000 0.010187000000000

1 commentaire

Leigh
Leigh le 3 Oct 2011
Thankyou! I used something slightly different but couldn't have done so without this answer!

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 3 Oct 2011

1 vote

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
Leigh le 3 Oct 2011
Cheers, I did want different boundaries, taking into account the # but I've figured it out. Thanks for your reply.

Connectez-vous pour commenter.

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!

Translated by