Ho to combine two legend in the same axe ( GUI MATLAB)
2 views (last 30 days)
Show older comments
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
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) )?
Accepted Answer
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?
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?
More Answers (0)
See Also
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!