Manually adding plots and legends with a Button Press

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

1 Comment

I've read something about
legend('-DynamicLegend');
(<http://undocumentedmatlab.com/blog/legend-semi-documented-feature>)
But not sure how it works

Sign in to comment.

Answers (2)

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 Comments

Thanks for your reply. But Im getting this error:
Undefined function 'eval' for input arguments of type 'cell'.
Also why so many ' in the:
txt1=['''' txt1 '''' ',' '''' legText '''']
because ' is required to define a string so you need two ' to define an apostrophe in a string and you need one apostrophe before and one after to define that it is a string (I know, complicated, but that's the way to work with strings in Matlab)
the error of cell type is probably because you get txt1 from the current legend. I think it is better if you store the txt1 each time in the handles structure so that you can use the correct format
so
txt1 = handles.txt1; % for this you first need to initialize an empty txt1 in the OpeningFcn of your gui
txt1=['''' txt1 '''' ',' '''' legText ''''];
eval(['hleg=legend(' txt1 ')']);
handles.txt1 = txt1;
% Update handles structure
guidata(hObject, handles);
Hi. Im still getting the same error:
Heres my code:
legText=get(handles.editLegend,'String') %get new legend text
txt1=get(legend(gca),'String'); %get current legend items
txt1=['''' txt1 '''' ',' '''' legText ''''];
eval(['hleg=legend(' txt1 ')']);Text
handles.txt1 = txt1;
% Update handles structure
guidata(hObject, handles);
In my openingFCN I have:
%To allow legend to manually change text colours
handles.txt1 = '';
% Update handles structure
guidata(hObject, handles);
you are still getting the current legend for txt1 (second line in your code) instead of using
txt1 = handles.tx1;
to set the value of txt1;
also it is not clear how you obtain the legText, is this from an editText field or is it a user defined field?
Hi, its from an edittext field
I now have my code as: legText=get(handles.editLegend,'String')
%txt1=get(legend(gca),'String');
txt1 = handles.tx1;
txt1=['''' txt1 '''' ',' '''' legText '''']
eval(['hleg=legend(' txt1 ')']);Text
handles.txt1 = txt1;
% Update handles structure
guidata(hObject, handles);
and get: Reference to non-existent field 'tx1'.
Error in ZemaxDistortion>pushbuttonTol_Callback (line 661)
txt1 = handles.tx1;
Ha, I see a typo here
txt1 = handles.tx1;
OK so I've sort of got it working but its still not right. I have added 2 graphs, names plot 1 and plot 2. But the legend reads
i.e. there is a data 1, data 2 and data 3 from somewhere?. Also the first legend item isn't plot 1.

Sign in to comment.

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 Comments

Thansk for this. I actually want the text thats entered in an edit box to be added.
I get the text from the edit box:
legText=get(handles.editLegend,'String')
and then when I change your concatenation line to
legendstr = [legendstr;[legText]];
I get the error
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
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;

Sign in to comment.

Asked:

on 6 May 2015

Commented:

on 7 May 2015

Community Treasure Hunt

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

Start Hunting!