remove the Text from the files
6 ビュー (過去 30 日間)
古いコメントを表示
Hi all,
i have a big number of fileswhich contains data with this structure. Is there any easy method to remove the Text and get only the numbers. Thank you!
Text
Text Text Text
5.420 12 0.6555
5.430 0 0.6698
2.440 5 0.6236
7.450 1 0.6324
9.460 15 0.6331
Text
Text Text Text
5.420 127 0.6555
5.430 0 0.6698
2.440 5 0.6236
7.450 1 0.6324
9.460 129 0.6331
Text
.....
...
2 件のコメント
Varun Pai
2015 年 10 月 14 日
can you please share one such file? What type of data is it ? Is that an imported file.
採用された回答
Stephen23
2015 年 10 月 14 日
編集済み: Stephen23
2015 年 10 月 14 日
k = 0;
opt = {'MultipleDelimsAsOne',true};
fid = fopen('temp.txt','rt');
tmp = textscan(fid,'%s',1);
while ~isempty(tmp{1}) && k<1000
k = k+1;
C(k,:) = tmp; %#ok<SAGROW>
D(k,:) = textscan(fid,'%s%s%s',1,opt{:}); %#ok<SAGROW>
M(k,:) = textscan(fid,'%f%f%f',opt{:}); %#ok<SAGROW>
%
tmp = textscan(fid,'%s',1);
end
fclose(fid);
It works because textscan will read only as much of the file as its format string matches. Every time it reaches the end of a block of text+numeric data we loop back to start again... until finally textscan does not match anything (i.e. the end of the file), then we stop.
The output cell arrays contain all of the data from the file:
>> C
C =
{1x1 cell}
{1x1 cell}
>> C{1}
ans =
'Text'
>> M
M =
[5x1 double] [5x1 double] [5x1 double]
[5x1 double] [5x1 double] [5x1 double]
>> M{1}
ans =
5.4200
5.4300
2.4400
7.4500
9.4600
Because the original question did not include any sample data-file I created this test file:
0 件のコメント
その他の回答 (1 件)
TastyPastry
2015 年 10 月 14 日
編集済み: TastyPastry
2015 年 10 月 14 日
Read in the first line, use str2num(). If the result is [], read the next line. If it's a numerical line, it should return a 1x3 vector. Store that. Continue reading in lines.
0 件のコメント
参考
カテゴリ
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!