Plot handle not being assigned

1 view (last 30 days)
Benjamin Z. Maloy
Benjamin Z. Maloy on 5 Oct 2021
Edited: Benjamin Z. Maloy on 6 Oct 2021
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 Comment
DGM
DGM on 5 Oct 2021
Edited: DGM on 5 Oct 2021
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 .

Sign in to comment.

Answers (1)

Bjorn Gustavsson
Bjorn Gustavsson on 5 Oct 2021
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

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!