Find the row of a word in a file
23 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
Titus Edelhofer
2012 年 2 月 27 日
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
0 件のコメント
その他の回答 (4 件)
Titus Edelhofer
2012 年 3 月 5 日
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
0 件のコメント
Tímea
2013 年 10 月 10 日
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 件のコメント
Tímea
2013 年 10 月 11 日
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
2013 年 10 月 12 日
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.
Tímea
2013 年 10 月 12 日
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!!
参考
カテゴリ
Help Center および File Exchange で Text Files についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!