Plot handle not being assigned

조회 수: 3(최근 30일)
Benjamin Z. Maloy
Benjamin Z. Maloy 2021년 10월 5일
편집: Benjamin Z. Maloy 2021년 10월 6일
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일
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

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by