Plot handle not being assigned

8 次查看(过去 30 天)
I have the following lines of code for plotting a couple of arrays, but I get the error:
Unable to perform assignment because the left and right sides have a different number of elements.
Error in ExtendDifferentDistance1to1 (line 179)
h(i+1)= plot(x, dev_low, 'o');
But I have confirmed that x and dev_low have the same number of elements, and so maybe I am a bit confused about what a handle is
% these have been normalized in excel
exp = xlsread(filename, 'B2:B102');
dev_low = xlsread(filename, 'C2:B102');
dev_up = xlsread(filename, 'D2:B102');
% plot the first wavelength you want to see so that we only have to call
% hold once
colors = {'r', 'b', 'y', 'black', 'c','g', 'm', '--r', '--b'};
h(1) = plot(x, aves{3}, colors{1});
hold on;
for i = 2:(length(y)-2)
h(i) = plot(x, aves{i+2}, colors{i});
end
% x indices are the same for experimental and simulated data
h(i) = plot(x, exp, '--black');
h(i+1)= plot(x, dev_low, 'o');
h(i+2)= plot(x, dev_up, 'o');
hold off;
DGM's answer is correct, dev_low and dev_up described more than one series!
  1 个评论
DGM
DGM 2021-10-5
编辑:DGM 2021-10-5
If my guess is right, the error isn't because your x and y data don't have matching geometry. It's because they describe more than one series (i.e. they aren't vectors). In that case, plot() will return more than one handle, which breaks the assignment to h. Consider the example
x = 1:10;
y = [1:10; 11:20];
h = plot(x,y)
h =
2×1 Line array: Line Line
If that's not the case, some placeholder data might be useful .

请先登录,再进行评论。

回答(1 个)

Bjorn Gustavsson
Bjorn Gustavsson 2021-10-5
When you plot multiple lines plot returns an array of line-handles then it doesn't work to assign those to one element in h. For this one can possibly use something like:
h_i = plot(x, exp, '--black');
h_ip1 = plot(x, dev_low, 'o');
h_ip2 = plot(x, dev_up, 'o');
Then one can use for example the first elements of h_i in calls to for examples legend:
legend([h(:);h_i(1);h_ip1(1);h_ip2(1)],'text','for','legend','etc')
HTH

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by