how to plot multiple graphs on one figure

[EDIT: 20110531 13:53 CDT - reformat - WDR]
i have three curves in one figure
values are like this :
  • x1 has the values from 10 to 30 and u have corresponding y1 values;
  • x2 has the values 10 to 60 and u have corresponding y2 values;
  • x3 has the values 10 to 90 and u have corresponding y3 values;
load myfig.txt
xdc1 = myfig(:,1);
ydc1 = myfig(:,2);
xdc2 = myfig(:,3);
ydc2 = myfig(:,4);
xdc3 = myfig(:,5);
ydc3 = myfig(:,6);
plot(xdc1,ydc1,xdc2,ydc2,xdc3,ydc3);
xlim([0 90]);
ylim([10 30]);
xlabel('Spark advance (degrees}');
ylabel('Power (kW)') ;
hleg1 = legend('dc = 30','dc = 60','dc = 90');
set(hleg1,'Location','SouthWest')
I AM GETTING THE FOLLOWING ERROR MESSAGE?
?? Error using ==> load
Number of columns on line 5 of ASCII file D:\ic\myfig.txt
must be the same as previous lines.
kindly tell me how to plot this graph

Respuestas (2)

Walter Roberson
Walter Roberson el 31 de Mayo de 2011

0 votos

Does your myfig.txt have some header lines?
The problem being reported is not with the plotting but right at the very beginning at the "load" statement. That kind of load() statement assumes that the input file just contains columns of numbers, the same number of columns on every line, and no text anywhere. If you have text anywhere, or if your columns are a fixed width (and might show up as blank to indicate 0 or missing data) then you need to read the data a different way.
The error message is telling you that line 5 of the file seems to have a different structure than lines 1-4.

4 comentarios

Vijaya
Vijaya el 31 de Mayo de 2011
i do not have text anywhere in the file. however the date is as i have appended here.
{
10 26.51885 10 21.13317 10 15.42834
15 26.72764 15 22.27656 15 16.70492
20 26.41123 20 23.20804 20 17.85618
25 25.5536 25 23.90087 25 18.86385
30 24.20111 30 24.3283 30 19.64495
35 24.4592 35 20.33191
40 24.25618 40 20.84709
45 23.68289 45 21.18852
50 22.70625 50 21.42901
55 21.33357 55 21.42537
60 19.54724 60 21.24232
65 20.86684
70 20.19544
75 19.40153
80 18.23229
85 16.74966
90 14.99123
}
actually x axis will have the value from 10 to 90
three curves with first will be plotted only for 10 to 30 and the next one for 10 to 60 and finally 10 to 90
all the three should come in one single figure
Vijaya
Vijaya el 31 de Mayo de 2011
in the first and second column there are only 5 values as 10,15,20,25,30
but in the fifth column 17 values as 10,15 ... 90
Walter Roberson
Walter Roberson el 31 de Mayo de 2011
You cannot use load() to read in a file that has that structure.
Vijaya
Vijaya el 31 de Mayo de 2011
can u pl help me to write the code for this particular problem. instead of load what should be used?

Iniciar sesión para comentar.

Matt Fig
Matt Fig el 31 de Mayo de 2011
There is probably a better way to do this, but here is what I did. I made a text file that looks like this (as you describe):
3 5 6
4 6 7
5 8 9
6 7 8
7 8 9
10
11
14
15
16
17
12
13
14
15
18
19
Then I imported it like this:
fid = fopen('myfile.txt');
T = textscan(fid,'%d%d%d')
T{3}(6:size(T{1},1)) = T{1}(6:end);
T{1} = T{1}(1:5);
T{2} = T{2}(1:5);
fclose(fid)
Now T{1} has the first column, T{2} has the second column, and T{3} has the third column as from the text file.

2 comentarios

Vijaya
Vijaya el 1 de Jun. de 2011
thank you very much for your answer.
i got the result as
T =
[28x1 int32] [28x1 int32] [27x1 int32]
ans =
0
However, i used the following commands and got the required graph.
{ fname = 'MYFILE.txt';
xdc1 = dlmread(fname, '\t', 'A1..A17');
ydc1 = dlmread(fname, '\t', 'B1..B17');
xdc2 = dlmread(fname, '\t', 'C1..C11');
ydc2 = dlmread(fname, '\t', 'D1..D11');
xdc3 = dlmread(fname, '\t', 'E1..E5');
ydc3 = dlmread(fname, '\t', 'F1..F5');
plot(xdc1,ydc1,xdc2,ydc2,xdc3,ydc3);
}
Thank you once again.
Walter Roberson
Walter Roberson el 1 de Jun. de 2011
The values are tab separated? And is there a tab for every empty value? If so then textscan() with 'Delimiter', '\t' and 'EmptyValue',nan). You would not even need to remove the nan() before plotting.

Iniciar sesión para comentar.

Preguntada:

el 31 de Mayo de 2011

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by