0
votes

capture of part of the .txt fileI need to create a figure window with two plots at the same time. I am given a .txt file with some introduction lines and 3 columns with values. The first column has values for x and the second and third values for y.

1st Access the file with fid=fopen etc.

2nd Read the file and skip the first 30 lines (introduction lines) which I did using this:

    headlines = fgetl(fid);
     for i = 1:30
         tline = fgetl(fid);
         headlines = char(headlines(1:i-1,:), tline);

3rd Making the plot itself, I'll use hold (or hold on) to make so that both plots get in the same figure window. Somehow I need to make a column be the gadering of values to put on the plot. I could write them by hand, but I need the program to read them itself. I can't quite explain it but I think you got the idea.

And last close it with fclose(fid) etc.

Could you please help me with the 3rd and tell me if something is missing? Thank you in advance :)

2
Actually, I don't understand what you mean. Can you draw an example in MSPaint and attach to the question?Andrey Rubshtein
Oli got it right. I have a lot of values which result in a lot of vectors, so I can't right them by hand (over a hundred). And what he answered solves my problem, though now I need to skip those 30 lines.Dywabo

2 Answers

2
votes

Ok, I Think I found the problem. I suspected that your data had some invalid text somewhere. I searched the internet and I think I found a copy of the data ... http://cdiac.ornl.gov/ftp/trends/temp/hansen/gl_land.txt

The ending of this data set is as follows:

 2007      0.75      0.69
 2008      0.56      0.70
 2009      0.72    -99.99
 2010      0.83    -99.99
-------------------------

The ---- line at the end is causing it to error.

So I used the following:

fid = fopen('gl_land.txt','r');
C = textscan(fid,'%f %f %f','headerlines',30,'commentstyle','--');
fclose(fid);
x = C{1}; y1 = C{2} y2 = C{3};
plot(x,[y1 y2])

I switched to textscan because textread does not support custom commentstyles. I used a comment style of '--' instead of just '-' because '-' would start skipping values that are negative.

Note: This data contains -99.99 to indicate invalid /missing data so you might want to add the following before plotting:

y1(y1==-99.99) = NaN;
y2(y2==-99.99) = NaN;

Data Plot

1
votes

I am not sure it exactly solves your problem. But i would do something like that:

values.txt :

...
...
1 0 0
2 0 1
3 1 0
4 1 1
5 1 2
6 2 4

matlab script:

[x y1 y2]=textread('values.txt','%f %f %f','headerlines',30)
plot(x,[y1 y2])

result :

enter image description here