0
votes

I have a text file containing following data:

2n10
B127 mg/dL  01:45 pm 3 of January
E83 mg/dL  10:03 am 3 of January
H97 mg/dL  11:05 pm 2 of January
K80 mg/dL  06:00 am 3 of January
P118 mg/dL  08:15 am 3 of January
S97
S80
S118
S81
S87
S85
S89
S82
S83
S127
a

And I want to read some (specified) data from this text file and then plot it. The data that I want to read and plot is:

S97
S80
S118
S81
S87
S85
S89
S82
S83
S127

How can I do that ?

Hints:

  • First part of file:

    2n10
    B127 mg/dL  01:45 pm 3 of January
    E83 mg/dL  10:03 am 3 of January
    H97 mg/dL  11:05 pm 2 of January
    K80 mg/dL  06:00 am 3 of January
    P118 mg/dL  08:15 am 3 of January
    

    Always consists of six rows, so the first line that I actually want to read and plot starts from the seventh row.

  • Second part of file (which I want to read and plot) may contain an arbitrary number of rows, but every row starts with character S and the end of file is always marked by character a.

Please help me ^_^

Thanks and best regards.

1
Those data to be neglected always have more than one word in those rows? - Divakar
And the valid rows (that are to be considered for plot) are always single string/word? - Divakar

1 Answers

0
votes

A solution might be this one:

fid = fopen('test.txt');

tline = fgets(fid);
while ischar(tline)
    parts = textscan(tline, 'S%d');
    if numel(parts{1}) > 0
        disp(['S' num2str(parts{1}) ])
    end
    tline = fgets(fid);
end

fclose(fid);

Basically, it opens the file and searches for lines with the format:

S< number >

For each one of these lines, it scans the number and prints the string without the newline.

Hope this helps.