Find the row of a word in a file
Mostra commenti meno recenti
Dear all, I have a text file, which has some headers and then 3 lists like the following:
Region_1
2.1 3.2 4.3
3.2 9.3 6.7
...
Region_2
1.1 1.2 0.3
5.5 9.7 9.7
...
And I want to take the table only for Region_23. How can I do that?
I hope you could help me with this, I didnt manage to find any commands...
All the best,
Panos
p.s. Every region has different nr of rows. They tables dont have the same size
Risposta accettata
Più risposte (4)
Panos
il 5 Mar 2012
Titus Edelhofer
il 5 Mar 2012
Hi Panos,
to read a number instead of a string, use textscan again but replace the %s by %f: let's say sFound is the line. Then
sFoundParts = textscan(sFound, '%s %f %f %f');
should give in sFoundParts{1} the "Total" in the other cells the numbers.
Titus
Tímea
il 10 Ott 2013
0 voti
Hi!
I have a very similar problem, most of the code worked perfect, thanks! I couldn't get this last part you wrote above working though: % and read from s{1}(idx1+1:idx2-1) the values using textscan again ...
My line was this: alpha = textscan(s{1}(idx1+1:idx2-1),'%f32',idx2-idx1-1)
Perhaps I misunderstand how it works with textscan..
What I finally want to do is to copy all the text between (idx1+1:idx2-1) to a vector, as floating point numbers. Do you have a suggestion for that?
Tim
3 Commenti
Cedric
il 10 Ott 2013
You could do something like:
buffer = s{1}(idx1+1:idx2-1) ;
dataTxt = sprintf( '%s\n', buffer{:} ) ;
and call TEXTSCAN with dataTxt, e.g.
data = textscan( dataTxt, '%f %f %f' ) ;
Tímea
il 11 Ott 2013
Thanks! Something happens, but I still can't get the numbers from 'data' out. The structure of the result I get is this:
data = [3112136x1 double]
>>data(1)
ans =
[3112136x1 double]
Probably it's not the way to do it, but I haven't found any other yet. This is how the code looks now:
if true
% code
fid = fopen('50.dx','rt');
% read the entire file - s becomes a cell array (12448587x1)
s = textscan(fid,'%s','delimiter','\n');
% search for your region (have to copy the whole row)
idx1 = find(strcmp(s{1},'object "alfa" class array type float rank 0 items 3112136 data follows'),1,'first');
% now search for the next region (-||-)
idx2 = find(strcmp(s{1},'attribute "dep" string "positions"'),1,'last');
% and read from s{1}(idx1+1:idx2-1) the values using textscan again
buffer = s{1}(idx1+1:idx2-1);
dataTxt = sprintf('%s\n',buffer{:});
% call TEXTSCAN with dataTxt, e.g.
data = textscan(dataTxt,'%f');
fclose(fid); end
(In the original text file I have numbers in one column, corresponding to scalar values for a 3d box domain, which I alter on want to plot.)
Tim
Cedric
il 12 Ott 2013
Check that dataTxt is a string which contains only numbers. It seems that TEXTSCAN outputs a cell array, which is a bit weird if you have only numbers and use the simple '%f' formatSpec. Yet, it's worth checking whether these numbers are what you are looking for.
1. Check that data is a cell array:
>> class(data)
if this return cell, you are dealing with a cell array. Then just extract the content of cell 1 (content of cells are addressed using curly brackets):
>> data = data{1} ;
Now data should be a numeric array that you can plot, reshape, etc.
Categorie
Scopri di più su Text Files in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!