legend problem with handles

Hi, Can someone explain me why the following code don't display the legend (I'm using Matlab R2016a):
markers(1, 5) = {'1'};
markers(2, 5) = {'2'};
markers(3, 5) = {'3'};
markers(4, 5) = {'4'};
markers(5, 5) = {'5'};
// other markers properties
prop_name(1) = {'LineStyle'};
prop_name(2) = {'Marker'};
prop_name(3) = {'MarkerSize'};
prop_name(4) = {'Color'};
prop_name(5) = {'DisplayName'};
graph_decomp_X = [handles.spectre_start:1:handles.spectre_stop];
graph_decomp = plot(handles.axesDecomposition, graph_decomp_X(1:marker_spacing:end), handles.graphe(1:marker_spacing:end, :));
set(graph_decomp, prop_name, markers);
legend('show')
Thanks, Bruno

 Accepted Answer

I'm betting that the result of the set aren't working as intended; you've got a 5x5 array for the markers array that at least in the above code snippet is empty for the first four columns so there aren't values for the properties matching all those in the prop_names vector.
If I just try something simple such as
hL=plot(randn(4,1),'k','linewidth',5,'displayname','Line 1');
legend('show')
it works just fine; I'm pretty sure if you debug the complex set command to ensure it actually sets the properties correctly then legend will also work.
We can't run your code as presented for lack of data for the compound variables and making something up for that which is missing would be time-consuming and a guess. Post a complete (simple) sample that illustrates the problem; I suspect in creating this you'll uncover the syntax errors in what you have.

6 Comments

Hi, thanks for your time but you presume wrong. The value for the others properties are written and were not put there since they are working correctly. The only thing I can't understand is why the legend isn't displayed with this code. The error message was something like "Plot empty".
Well, the crystal ball is often in error when it has little data from which to operate.
If the plot is empty as the error says, suspect that it's so and so there's nothing for legend to legend about.
Again, post us a sample that reconstructs the problem that we can run; not much else we can say otherwise other than demonstrated here that a proper plot will work as expected so it's bound to be an error somewhere in your code.
Set a breakpoint and ensure you actually have data when you create the plot; retrieve the X|YData from the line handles, etc., etc., etc., ...
I made it work, the solution was to do this:
legend (handles.axesDecomposition, 'show')
I figured it by making an independent version of the code so you did help in some way.
That would indicate that handles.axesDecomposition is/was not the current axes; the error message would indicate that whatever is gca was empty. Is this intended or is there an extra axes object hanging around somewhere?
yes, there is other axes objects in the GUI.
The moral of the story is to always use the handle for the desired object to be addressed if there's any chance at all for the focus to be somewhere else.

Sign in to comment.

More Answers (0)

Asked:

B A
on 27 Feb 2017

Commented:

dpb
on 1 Mar 2017

Community Treasure Hunt

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

Start Hunting!