Manually adding plots and legends with a Button Press
조회 수: 7 (최근 30일)
이전 댓글 표시
I have a button call back that I want to plot a set of data in a particular colour and add the legend. I then want to be able to press the button and each time a different data set is plotted in a different colour and its legend text to match the colour.
I First count the number of times the button has been pressed (its initially set to 1)
nplts=getappdata(0,'NominalPlotcount')
%Then I define the colour of my plot depending on this value.
nplts=getappdata(0,'NominalPlotcount')
switch nplts
case 1
colour='r'
case 2
colour='b'
case 3
colour='g'
case 4
colour='k'
otherwise
colour='r'
end
I then perform the plot and increment my plot counter nplts, and add the legend.
plot(x,N(:,1),colour,'MarkerSize', 4, 'Marker','o','LineWidth',1)
legText=get(handles.editLegend,'String');
legend(legText)
setappdata(0,'NominalPlotcount',nplts+1);
My legend doesn't concatenate to whats already there. I have tried
txt1=get(legend(gca),'String');
txt1=[txt1,legText];
hleg=legend(txt1);
But this just adds all the legend items in a single colour. Im really stuck now, any suggestions please?
Thanks Jason
답변 (2개)
Ingrid
2015년 5월 6일
if I understand your question correctly this should work, since you need to be placing brackets around each legend entry for matlab to interpret it is separate entries and hence assign a different colour
txt1=get(legend(gca),'String');
txt1=['''' txt1 '''' ',' '''' legText '''']
eval(['hleg=legend(' txt1 ')']);
댓글 수: 6
Joseph Cheng
2015년 5월 6일
why not try something like this out
clc; clear all;
nplts = 0;
legendstr = [];
figure(1),hold on;
cont = input('plot another rand plot?: ','s');
while ~isempty(cont)
nplts = nplts+1;
switch mod(nplts,5)
case 1
colour='r'
case 2
colour='b'
case 3
colour='g'
case 4
colour='k'
otherwise
colour='r'
end
plot(randi(1000,1,100),colour),legend('show');
hlegend = findobj(gcf,'Type','axes','Tag','legend');
legendstr = [legendstr;['another plot ' num2str(nplts)]];
% legendstr =get(legend(gca),'String');
legend(legendstr)
cont = input('plot another rand plot?: ','s');
end
댓글 수: 2
Joseph Cheng
2015년 5월 7일
well you would then convert what you want into a cell array which is a few edits to the example code of:
legendstr = {}; %note the { brackets
%and then down below
legendstr(nplts) = %user input text;
참고 항목
카테고리
Help Center 및 File Exchange에서 Legend에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
