text import - number of rows

Hi, I have the following code to read in a bunch of data from a textfile as shown below. In this example, the number of rows is 5. But, I don't always know the number of rows, so I want the code to read all the rows and calculate how many automatically. Anyone know how to?
n=5;
filename1='import.asc'
fid = fopen(filename1);
Q{i,1}=textscan(fid,'%u %f %f %f %f %f %f',n,'headerlines',rad(i)+4);
fclose(fid);
//////////////////////////////////////////////////////////////////////////////////////////
127 -0.29784E-03 0.64028E-06-0.23385E-02 0.24409E-06 0.13348E-06-0.69277E-03
128 -0.33237E-03-0.43242E-06-0.33875E-02 0.82221E-06 0.24963E-06-0.14770E-02
129 -0.40024E-03-0.28975E-06-0.52177E-02 0.40892E-06 0.23521E-06-0.23949E-02
130 -0.49607E-03-0.50922E-06-0.79652E-02 0.69354E-06 0.49298E-06-0.35142E-02
131 -0.61997E-03-0.63708E-06-0.11812E-01 0.92476E-06 0.94470E-06-0.49007E-02
///////////////////////////////////////////////////////////////////////////////////////////

2 Comments

Can you please upload a sample file. Click the paperclip button, then both Choose file and Attach file buttons.
Is this block the only text in the file? Are there multiple blocks? Do the blocks repeat? Are their header lines above each block?
Ok, here are two files (matlab script and textfile).
No, there are other text other than the bocks of interest. Yes, there are multiple blocks. I'm reading in all the blocks. The only task left is not having to enter number of rows (in this example 414).

Sign in to comment.

 Accepted Answer

Star Strider
Star Strider on 1 Feb 2016
See if my solution in Importing a .csv to matlab, delimiter problem will work in your application.

5 Comments

This will read — and parse — the first 415 lines, up to the first break. You will need to re-start it for the others. The read and parsed data are in the ‘DataParse’ numeric (double) matrix.
Use a loop and the fseek function to restart my code (labeled BLOCK READ LOOP), if you want to read the other numeric blocks. However you will need to put the fclose call last, outside your outer loop, to do that. I’m not certain how you want to separate the blocks and save them, or I would have included the outer loop.
The code:
fidi = fopen('nilsotto unit_load_data.txt','r');
% BLOCK READ LOOP START
for k1 = 1:27
L = fgetl(fidi);
end
L = fgetl(fidi);
k1 = 0;
IxV = [5 16 17 28 29 40 41 52 53 64 65 76];
while ~(feof(fidi))
k1 = k1 + 1;
L = fgetl(fidi);
if isempty(L)
break
end
fields = length(regexp(L, ','))+1;
Data = textscan(L, repmat('%s', 1, fields), 'Delimiter',',');
DataStr = char(Data{:});
DataParse(k1,:) = str2num([DataStr(1:3) ' ' DataStr(IxV(1):IxV(2)) ' ' DataStr(IxV(3):IxV(4)) ' ' DataStr(IxV(5):IxV(6)) ' ' DataStr(IxV(7):IxV(8)) ' ' DataStr(IxV(9):IxV(10)) ' ' DataStr(IxV(11):IxV(12))]);
end
% BLOCK READ LOOP END
fclose(fidi);
Many thanks Star Strider. You are my hero.
As always, my pleasure!
This is a great code. thanks
My pleasure!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!