0

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.

Anouk
  • 1
  • 1

1 Answers1

0
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);
Adriaan
  • 17,741
  • 7
  • 42
  • 75
Anouk
  • 1
  • 1
  • Please read [ask] and [edit] your question to contain an explanation as to why this code would actually solve the problem at hand. Always remember that you're not only solving the problem, but are also educating the OP and any future readers of this post. – Adriaan May 16 '22 at 05:19