Ho to combine two legend in the same axe ( GUI MATLAB)
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Geoff Hayes
2014-7-21
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) )?
采纳的回答
Geoff Hayes
2014-7-22
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?
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Legend 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!