remove the Text from the files

6 views (last 30 days)
Rica
Rica on 14 Oct 2015
Edited: Stephen23 on 14 Oct 2015
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
Varun Pai on 14 Oct 2015
can you please share one such file? What type of data is it ? Is that an imported file.
Rica
Rica on 14 Oct 2015
it is a .txt file

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 14 Oct 2015
Edited: Stephen23 on 14 Oct 2015
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:

More Answers (1)

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

Community Treasure Hunt

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

Start Hunting!