open text file constructed in a specified manner

2 visualizzazioni (ultimi 30 giorni)
Rica
Rica il 18 Mar 2015
Modificato: dpb il 19 Mar 2015
Hi all,
I have a big text file in this construction:
blablblala
blablabla
1 2 3
4 5 6
2 5 8
blablblala
blablabla
5 8 9
5 2 3
2 5 6
blablblala
blablabla
2 5 8
98 56 42
5 8 7
blablblala
blablabla
...
...
...
blablblala
blablabla
...
...
...
blablblala
blablabla
.
.
.
the structure is the same , only the numbers do change. ho could open this kind of text file and get the data in a cell:
C{1}=
1 2 3
4 5 6
2 5 8
C{2}=
5 8 9
5 2 3
2 5 6
C{3}=...
and so on.
Thank you.
  1 Commento
per isakson
per isakson il 18 Mar 2015
The strings
blablblala
blablabla
do they contain substrings, which can be used as indicators of
START OF NUMERICAL DATA
and
END OF NUMERICAL DATA

Accedi per commentare.

Risposte (2)

dpb
dpb il 18 Mar 2015
textscan

per isakson
per isakson il 18 Mar 2015
Modificato: per isakson il 18 Mar 2015
  4 Commenti
per isakson
per isakson il 18 Mar 2015
Modificato: per isakson il 18 Mar 2015
dpb, I just tested your use of textscan. It's the first time I use textscan this way. A mental hurdle to me has been that I've regarded formatSpec not matching and textscan terminating as kind of an "error". Your construct is based on these two excerpts from the documentation.
throughout the entire file and stops when it cannot match formatSpec
to the data.
textscan automatically resumes reading at the point where it terminated
the last read
Next I had to try to replace 'Headerlines',5 by
[cac,pos] = textscan( fid, '%[^Start]');
i.e. advance the pointer to the position left of Start. Either I've run into an "issue" or I've misinterpreted the documentation. I'll try again later.
&nbsp
"just the titles to show up" &nbsp The example below will display MyTitle
<http://myweb.mypage.com/ MyTitle>
dpb
dpb il 18 Mar 2015
Modificato: dpb il 19 Mar 2015
This one is almost trivial, Per...altho one thing I dislike about textscan is that it won't figure out the number/line automagically so if you don't know that you have to do some work to discern that for the format string. But, if it is known a priori, you can do this one by inspection on the fly in one try...
>> type blabla.txt
blablblala
blablabla
1 2 3
4 5 6
2 5 8
blablblala
blablabla
5 8 9
5 2 3
2 5 6
blablblala
blablabla
2 5 8
98 56 42
5 8 7
>> fid=fopen('blabla.txt');
>> fgetl(fid);
>> while ~feof(fid)
cell2mat(textscan(fid,'%f%f%f','headerlines',2', ...
'collectoutput',1))
end
ans =
1 2 3
4 5 6
2 5 8
ans =
5 8 9
5 2 3
2 5 6
ans =
2 5 8
98 56 42
5 8 7
>> fid=fclose(fid);
>>
The only "trick" used here is fgetl to throw away the '%%' line. You would think that 'commentstyle','%' would take care of that, but no; the 'headerlines',2 option takes over first and "eats" those first two lines so then there are no comment lines in the file after that. I think that's a debatable implementation decision, but it's hard to argue it's really a bug.
@Per--I think you're overthinking textscan. For files like the above simply look for the regularity and what it takes to
  1. get past any unique header information first, and then
  2. write a format expression (or group thereof) that handles the repetitive block as if it were the only one in the file and finally,
  3. repeat that block until done.
In the above he '%f' field will repeat until it fails; using three simply causes it to return the result as an array of three columns instead of as a vector; the file pointer will be at the beginning of the next header line where the failure to convert to a numeric value occurred so then the repeat of skipping the two headerlines picks up and "lather, rinse and repeat"...

Accedi per commentare.

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by