remove the Text from the files

10 次查看(过去 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
Varun Pai 2015-10-14
can you please share one such file? What type of data is it ? Is that an imported file.

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2015-10-14
编辑:Stephen23 2015-10-14
This is fast and easy using textscan in a loop:
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:

更多回答(1 个)

TastyPastry
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.

类别

Help CenterFile Exchange 中查找有关 Text Data Preparation 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by