multiple legend not showing in plot

I have 5 plots and try to show the legend using handle.
h= [plot1(1);plot2(2);plot3(3);plot4(4);plot5(5)];
legend(h,'pln1','pln2','pln3','pln4','pln5');
But I get this error:
Index exceeds the number of array elements (1).
I appreciate any help!

4 comentarios

Chris Burschyk
Chris Burschyk el 8 de Jul. de 2022
What is plot1, plot 2...etc?
Without more of your code, trying to answer your question would be fortune telling :)
dpb
dpb el 8 de Jul. de 2022
Well, I'll get our the ouija board and give it a go, @Chris Burschyk! :)
From the error message, one can infer the various plotN are line handles from previous calls to plot() (a BADIDEA, btw, @Ham Man to use such naming conventions in MATLAB as the need for the line shows), in which case there really isn't a plot2(2) but only one line handle per variable.
If really have five plots on different axes, I've not tried the array of line handles to legend across axes; I doubt it will work when does get that far. If, instead, they were five lines plotted on a single axes with hold on in effect, then fixing the indexing would/should work.
Why the thinking there's a need for a subscript here at all is somewhat baffling, though...dunno what OP must be thinking.
Karim
Karim el 10 de Jul. de 2022
Editada: Karim el 10 de Jul. de 2022
Note that you create a lot of room for intepretation on how you construct 'plot1' and what the content is, this makes it very difficult for people to help.
If you created the 'plot1' etc as seperate figures (or perhaps as plot or line commands) then you cannot acces plot3(3) since there will not be a third element. This can result in the indexing error. In such case you can acces all of them as plot3(1), see below for an example.
If this doesn't help you will need to give more information on how (and why) you construct 'h' in this particular manner and what the content of plot1 etc is.
plot1 = figure('visible','off'); plot(1:10,rand(10,1));
plot2 = figure('visible','off'); plot(1:10,rand(10,1));
plot3 = figure('visible','off'); plot(1:10,rand(10,1));
% this can work
h = [plot1(1);plot2(1);plot3(1)]
h =
3×1 Figure array: Figure (1) Figure (2) Figure (3)
% this will produce an indexing error
h = [plot1(1);plot2(2);plot3(3)]
Index exceeds the number of array elements. Index must not exceed 1.
Ham Man
Ham Man el 10 de Jul. de 2022
Great Karim,Many thanks, it works

Iniciar sesión para comentar.

Respuestas (0)

Etiquetas

Preguntada:

el 8 de Jul. de 2022

Comentada:

el 10 de Jul. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by