Ho to combine two legend in the same axe ( GUI MATLAB)

1 view (last 30 days)
Hello,
In my interface, I call two plot,in the same plot function. This is the reference1 and renference2.
function is updated at each call. I dont want that the legend is also updated
What is the solution to manage legend.
if ishandle(tracesnew.handleTrace1) %
set(tracesnew.handleTrace1, 'XData', x, 'YData', y)
else
traces.handleTrace1 = plot(x,y,coul)
end
legend( traces.handleTrace1,'refereence1','reference2') ;
For the second call, I'd like renference2 is also displayed.
  2 Comments
Geoff Hayes
Geoff Hayes on 21 Jul 2014
From the above code, it appears that on the second time that a function is called, the data that has been originally plotted is replaced with the new x and y data. It is unclear why you would wish to display two messages in your legend since only one plot remains. Or is something else plotted on the same axes?
Does reference 1 correspond to the first plot ( traces.handleTrace1 = plot(x,y,could) ), and reference 2 the second ( set(tracesnew.handleTrace1, 'XData', x, 'YData', y) )?
bekoto raz
bekoto raz on 21 Jul 2014
Yes, normally, 4 plotted must be on the same axes. With this, 4 legend should appear on the axes when I call the button.
the problem is that the plot function is updated at each call.
when I do after two call:
function [call] = called(x,y,coul,auto,callnew)
if ( auto==1) % first call
traces.handleTrace1 = plot(x,y,'g')
legend([traces.handleTrace1],{'reference1'})
end
if (auto==2) % second call
traces.handleTrace2 = plot(x,y,'k')
legend([traces.handleTrace1 traces.handleTrace2],{'reference1','reference2'})
end
in this exemple, when I call the first button, I have a curve like reference1 Legend ;but when I call the second, I have a second curve , and the only legend which appeared is reference1 with black line.
I'd like to bring up two legends with two curves, or 4 for 4 legends curves.
Do you have a solution to this report.
thank you.

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 22 Jul 2014
If I try to run your code, it fails with the
Error using legend>process_inputs (line 564)
Invalid argument. Type 'help legend' for more information.
because the strings are wrapped in braces, making the second input a cell array. Just passing as individual strings is sufficient (see legend for details).
As well, it is unclear how the traces variable maintains its values between function calls - it is neither an input (or output) nor a global variable, so how is it expected t be used?
One solution is to define it as a persistent variable
function [call] = called(x,y,coul,auto,callnew)
persistent traces;
call = [];
if ( auto==1) % first call
traces.handleTrace1 = plot(x,y,'g');
legend([traces.handleTrace1],{'reference1'});
hold on;
elseif (auto==2) % second call
traces.handleTrace2 = plot(x,y,'k');
legend([traces.handleTrace1 traces.handleTrace2],'reference1','reference2');
end
Running the above allows the figure to be updated with the first plot and legend (green) when auto is 1. Upon a subsequent call to this function, the second plot is drawn (note the hold on statement so that the original is "kept") and the legend is updated for both plots (green and black).
As well, how is the output variable call used? What is the purpose of the inputs coul and callnew?
  1 Comment
bekoto raz
bekoto raz on 22 Jul 2014
Thank you, your code running correctly.
function [call] = called(x,y,coul,auto,callnew)
is called in the Gui interface like this
auto=1;
handles.call= called(vect1,vect2,'-m',auto,handles.call)

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!