remove the Text from the files
6 views (last 30 days)
Show older comments
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 Comments
Varun Pai
on 14 Oct 2015
can you please share one such file? What type of data is it ? Is that an imported file.
Accepted Answer
Stephen23
on 14 Oct 2015
Edited: Stephen23
on 14 Oct 2015
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 Comments
More Answers (1)
TastyPastry
on 14 Oct 2015
Edited: TastyPastry
on 14 Oct 2015
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 Comments
See Also
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!