Reading data into Matlab

2 Ansichten (letzte 30 Tage)
Danny Coles
Danny Coles am 15 Nov. 2013
Bearbeitet: dpb am 15 Nov. 2013
Hi,
I need to read specific lines from a text file into Matlab, the text file is in the following format...
2 4 6 7 3
5 5 3 2 2 new boundary
5 3 2 4 4 new boundary
7 4 4 6 7
I want to be able to read into Matlab the lines that start with a 5, so in this case lines 2 and 3. In other scenarios the lines that start with a 5 will be on different lines to 2 and 3, so I need to specify which lines to read based on the first number in that line being a 5. It would also be useful if the solution to this problem only reads the numbers on the correct lines and not the text 'new boundary' that appears on these lines.
Any help on how to do this would be greatly appreciated.
Thanks
Danny

Antworten (1)

dpb
dpb am 15 Nov. 2013
Bearbeitet: dpb am 15 Nov. 2013
Unless the overall file is huge, probably the easiest way to deal with this (and likely fastest as well) would be to simply read the whole file into memory and save the lines desired...
>> fid=fopen('danny.txt');
>> c=cell2mat(textscan(fid,[repmat('%d',1,5) '%*[^\n]'],'collectoutput',1))
c =
2 4 6 7 3
5 5 3 2 2
5 3 2 4 4
7 4 4 6 7
>> c=c(c(:,1)==5,:)
c =
5 5 3 2 2
5 3 2 4 4
>> fid=fclose(fid);

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by