0
votes

I have a text file which consists:

// MATLAB to process this file
ModelName : Matlab1
PerturbationId : 0
X1X2_001 : 0.270971584968118
X1X2_002 : 0.55555
OBJECTF : ?
NumberOfRuns : 25
SimulationId : 0

What I want to do is to get 0.270971584968118 and 0.55555 out of it, the important thing is that the file is not the same after each run, I mean the name of X1X2 may change (but there is always 001 and 002 at the end). also the values may change (0.270971584968118 and 0.55555). I used textscan but since the file has not specific format (values are not separated by tab, and they are not tabular) it was not useful.

Thanks

1
Did you try using textscan? - Daniel
if you read in the file and set delimiter to ":", you are looking at the 6th and 8th (or (3,2),(4,2)) th element. - GameOfThrows
yes I have tried textscan, but I do not know how to do it - masoud
I have provided you a basic solution - GameOfThrows
Thank you so much guys, it worked ;) - masoud

1 Answers

0
votes

OKAY. So the straightforward way says:

%//read in file
fileID = fopen('your.txt')
C = textscan(fileID,'%s %s','Delimiter',':');  %// delimit on ':'
%//you are looking for C{2} the second row of the cell.
value1 = cell2mat(C{2}(4,:));
>> 0.270971584968118
value2 = cell2mat(C{2}(5,:));
>> 0.55555

So this is quite straight forward and easy to understand, but it is not the most efficient way. For efficiency, look for alternatives to cell2mat.