text import - number of rows

2 views (last 30 days)
nilsotto
nilsotto on 1 Feb 2016
Commented: Star Strider on 5 Jul 2017
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
Stephen23
Stephen23 on 1 Feb 2016
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?
nilsotto
nilsotto on 2 Feb 2016
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
Gabriel Cortes
Gabriel Cortes on 5 Jul 2017
This is a great code. thanks

Sign in to comment.

More Answers (0)

Categories

Find more on Text Data Preparation in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!