Anouk
Anouk

Reputation: 1

MatLab textscan(), from messy format with missing lines

I'm not familiar with MatLab textscan(). Any help you could provide would be much appreciated.

The text file that I want to read looks as follows:

test.0.xml
myFunc(x) = 0.0294118
execution time: 5.87ms
test.1.xml
myFunc(x) = 0.0625
execution time: 1.618ms
test.10.xml
test.11.xml
test.12.xml
myFunc(x) = 0.0416667
execution time: 0.38ms
test.13.xml
myFunc(x) = 0.0625
execution time: 7.076ms
test.14.xml
myFunc(x) = 0.0384615
execution time: 10.51ms
...

Preferably the result would be formatted similar to this:

test = [0, 1, 10, 11, 12, 13, ...];
myFunc = [0.0294118, 0.0625, NaN, NaN, 0.0416667, 0.0625, ...];
executionTime = [5.87, 1.618, NaN, NaN, 0.38, 7.076, ...];

Thank you in advance.

Upvotes: 0

Views: 60

Answers (1)

Anouk
Anouk

Reputation: 1

fileID = fopen('file.txt');

missing = false;
test = [];
myFunc = [];
eTime = [];

while ~feof(fileID)
    thisLine = fgetl(fileID);
    if size(thisLine) >= 1
        if thisLine(1) == 't'
            if missing == true
                myFunc(end+1) = NaN;
                eTime(end+1) = NaN;
            end
            test(end+1) = sscanf(thisLine,'test.%d');
            missing = true;
        elseif thisLine(1) == 'm'
            myFunc(end+1) = sscanf(thisLine,'myFunc(x) = %f');
            missing = false;
        else
            eTime(end+1) = sscanf(thisLine,'eTime: %f');
            missing = false;
        end
    end
%     disp(thisLine)
end

fclose(fileID);

Upvotes: 0

Related Questions