Plot Connecting at both ends

5 visualizzazioni (ultimi 30 giorni)
Alyssa Tijerina
Alyssa Tijerina il 17 Ott 2018
Commentato: Yaren il 20 Lug 2022
Hello.
I am trying to graph 10 columns of data (wavelength on the x axis and reflectance on the y axis), but it is adding a line from the first point to the last point. It is also adding more than the amount of plots. I only included two columns but it has 20 lines?
filename='Leaf Graph.xlsx';
table1=readtable(filename);
data=table2array(table1);
data1=data(:,1);
data2=data(:,2);
data3=data(:,3);
data4=data(:,4);
data5=data(:,5);
data6=data(:,6);
data7=data(:,7);
data8=data(:,8);
data9=data(:,9);
data10=data(:,10);
data11=data(:,11);
x=data1;
plot(x,data2)
hold on
plot(x,data3)

Risposte (1)

Guillaume
Guillaume il 17 Ott 2018
matlab plots exactly what you give it. I can see only two curves plotted in your screenshot. The reason they would wrap back from 2500 to 400 is because that's what happens in your first column, i.e your x is going to be something like
[400
500
...
2400
2500
400
500
...
2500
400
...
]
Note that if you did the plot in excel, you'd get exactly the same result.
If you don't want your plot to wrap around, then you'll have to sort/average/whatever your data so it is monotonically increasing.
Now, let's talk about the horror that is your code. A simple rule: numbered variables is always wrong. You have a perfectly usable array, why go and split it up into many variables. The exact same result that you had could be obtained with just one line:
plot(data(:, 1), data(:, [2 3]); %only plot column 2 and 3 against 1
and you could plot all 10 columns with also just one line:
plot(data(:, 1), data(:, 2:end)); column 2-11 against column 1
Possibly a simple way to solve your wrap around is to sort the rows of your array against the first column:
data = sortrows(data, 1);
plot(data(:, 1), data(:, 2:end));
And you don't even need to convert the table to an array:
table1 = readtable(filename);
table1 = sortrows(table1, 1);
plot(table1{:, 1}, table1{:, 2:end});
  1 Commento
Yaren
Yaren il 20 Lug 2022
Does not solve the problem that Matlab connects the edges :'/
c = [0,1,2];
d = [3,1,2];
plot(c,d)
Yet, it sometimes does not do this I do not know why.

Accedi per commentare.

Categorie

Scopri di più su 2-D and 3-D Plots in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by