Find the row of a word in a file

23 views (last 30 days)
Panos
Panos on 27 Feb 2012
Commented: Cedric on 12 Oct 2013
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

Accepted Answer

Titus Edelhofer
Titus Edelhofer on 27 Feb 2012
Hi,
something like the following should do the trick
fid = fopen('yourtextfile.txt', 'rt');
% read the entire file, if not too big
s = textscan(fid, '%s', 'delimiter', '\n');
% search for your Region:
idx1 = find(strcmp(s{1}, 'Region_23'), 1, 'first');
% now search for the next Region:
idx2 = find(strcmp(s{1}, 'Region_24'), 1, 'first');
% and read from s{1}(idx1+1:idx2-1) the values using textscan again ...
fclose(fid);
Not tested but should give you a hint ...
Titus

More Answers (4)

Panos
Panos on 5 Mar 2012
Hi Titus,
thnx for you answer. It did exactly what I wanted. I d like to ask you something else. How is it possible to read from a file a line removing or ignoring a word, and reading the existing numbers not as comments. Because as it is, it reads the numbers as comments. The line looks like this:
Total 333.398E-03 5.42046E-03 604.137E-03
I use strrep to remove the 'Total' word. But how do I take the numbers as a table lets say of 3 numbers?
All the best,
Panos

Titus Edelhofer
Titus Edelhofer on 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
Tímea on 10 Oct 2013
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 Comments
Tímea
Tímea on 11 Oct 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
Cedric on 12 Oct 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.

Sign in to comment.


Tímea
Tímea on 12 Oct 2013
Ah, the bracket was the trick! Everything worked like you wrote above, data was a cell array. I tried cell2mat before, that didn't worked the way I tried.
Thanks a lot!!

Tags

Community Treasure Hunt

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

Start Hunting!