0

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.

2
  • Those data to be neglected always have more than one word in those rows? Commented Apr 28, 2014 at 13:17
  • And the valid rows (that are to be considered for plot) are always single string/word? Commented Apr 28, 2014 at 13:30

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much Sir Antonio L ^_^ Please , when I run your excellent program I get these Data : S97 S80 S118 S81 S87 S85 S89 S82 S83 S127 The variable of these data are tline or parts ? How can I remove the the char ( S ) and then plot it ? Regards ...
parts{1} contains the number itself. If you want to print the numbers without the S, just delete the 'S' in the call to disp. Regards.
Thanks Sir Antonio ^_^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.