I would need some help regarding a simple GUI I am working on.
I have a GUI with two plots : Pressure and Temperature and 3 sets of data each one containing one pressure curve and one temperature curve. I have on top of that, 3 checkbox that enable me to select the set of data I want to plot.
My question now is : I want the legend to be automatically updated regarding the set of data I have selected using the checkbox. That's mean adding new legend if a checkbox is selected on top of existing one but also removing the legend when checkbox is unselected.
Currently this is what I have as code when the GUI is opening:
function GUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to GUI (see VARARGIN)
handles.Data=load('DataCell');
cc=Lines(3);
hold(handles.PressureGraph);
grid(handles.PressureGraph,'on')
xlabel(handles.PressureGraph,'Time [s]')
ylabel(handles.PressureGraph,'Pressure [bars]')
handles.p1=plot(handles.PressureGraph,handles.Data.DataCell(:,1),handles.Data.DataCell(:,2),'color',cc(1,:),'DisplayName','1');
set(handles.p1, 'Visible', 'off');
handles.p2=plot(handles.PressureGraph,handles.Data.DataCell(:,4),handles.Data.DataCell(:,5),'color',cc(2,:),'DisplayName','2');
set(handles.p2, 'Visible', 'off');
handles.p3=plot(handles.PressureGraph,handles.Data.DataCell(:,7),handles.Data.DataCell(:,8),'color',cc(3,:),'DisplayName','3');
set(handles.p3, 'Visible', 'off');
hold(handles.TempGraph);
grid(handles.TempGraph,'on')
xlabel(handles.TempGraph,'Time [s]')
ylabel(handles.TempGraph,strcat('Temperature [',char(176),']'))
handles.t1=plot(handles.TempGraph,handles.Data.DataCell(:,1),handles.Data.DataCell(:,3),'color',cc(1,:),'DisplayName','1');
set(handles.t1, 'Visible', 'off');
handles.t2=plot(handles.TempGraph,handles.Data.DataCell(:,4),handles.Data.DataCell(:,6),'color',cc(2,:),'DisplayName','2');
set(handles.t2, 'Visible', 'off');
handles.t3=plot(handles.TempGraph,handles.Data.DataCell(:,7),handles.Data.DataCell(:,9),'color',cc(3,:),'DisplayName','3');
set(handles.t3, 'Visible', 'off');
legend(handles.PressureGraph,'hide');
legend(handles.TempGraph,'hide');
% Choose default command line output for GUI
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
And now for each checkbox call function I have the following code taken from previous forums :
function checkbox1_Callback(hObject, eventdata, handles)
% hObject handle to checkbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% guidata(hObject, handles);
visstates = {'off', 'on'};
thisisvis = visstates{1 + get(hObject, 'Value')};
set(handles.p1, 'Visible', thisisvis);
set(handles.t1, 'Visible', thisisvis)
I am using the property 'DisplayName' for each plot to store the legend string. I looked on different topics and I guess there is something to put in place thanks to '-DynamicLegend' but no matter what I do, it is always showing the 3 legends on each plot even if only 1 checkbox is selected.
Any help would be very much appreciated.
Thanks.
Edit :
Benoit, I tried what you have suggested by creating a handles.LegendStrings Variable which contains a simple string for each of the 28 sets of data (and each set of data contains 6 lines) :
for k=1:6
for i=1:28
handles.PressurePlots{i,k}=plot(handles.PressureGraph,handles.Data.Data{i,k}(:,1),handles.Data.Data{i,k}(:,2),'-',[handles.Data.Data{i,7}+handles.Data.Data{i,14} handles.Data.Data{i,7}+handles.Data.Data{i,14}],[min(handles.Data.Data{i,k}(:,2)) max(handles.Data.Data{i,k}(:,2))],'--',[handles.Data.Data{i,8}+handles.Data.Data{i,14} handles.Data.Data{i,8}+handles.Data.Data{i,14}],[min(handles.Data.Data{i,k}(:,2)) max(handles.Data.Data{i,k}(:,2))],'--',[handles.Data.Data{i,9}+handles.Data.Data{i,14} handles.Data.Data{i,9}+handles.Data.Data{i,14}],[min(handles.Data.Data{i,k}(:,2)) max(handles.Data.Data{i,k}(:,2))],'--',[handles.Data.Data{i,10}+handles.Data.Data{i,14} handles.Data.Data{i,10}+handles.Data.Data{i,14}],[min(handles.Data.Data{i,k}(:,2)) max(handles.Data.Data{i,k}(:,2))],'--',[handles.Data.Data{i,11} handles.Data.Data{i,11}],[min(handles.Data.Data{i,k}(:,2)) max(handles.Data.Data{i,k}(:,2))],'--',[handles.Data.Data{i,12}+handles.Data.Data{i,14} handles.Data.Data{i,12}+handles.Data.Data{i,14}],[min(handles.Data.Data{i,k}(:,2)) max(handles.Data.Data{i,k}(:,2))],'--',[handles.Data.Data{i,13}+handles.Data.Data{i,14} handles.Data.Data{i,13}+handles.Data.Data{i,14}],[min(handles.Data.Data{i,k}(:,2)) max(handles.Data.Data{i,k}(:,2))],'--','color',cc(i,:),'DisplayName',num2str(i));
handles.LegendStrings{i}=num2str(i);
set(handles.PressurePlots{i,k}, 'Visible', 'off');
end
end
Then creating my GetCheckBoxes vector :
GetCheckboxes = [get(handles.checkbox1,'Value') get(handles.checkbox2,'Value') get(handles.checkbox3,'Value') get(handles.checkbox4,'Value') get(handles.checkbox5,'Value') get(handles.checkbox6,'Value') get(handles.checkbox7,'Value') get(handles.checkbox8,'Value') get(handles.checkbox9,'Value') get(handles.checkbox10,'Value') get(handles.checkbox11,'Value') get(handles.checkbox12,'Value') get(handles.checkbox13,'Value') get(handles.checkbox14,'Value') get(handles.checkbox15,'Value') get(handles.checkbox16,'Value') get(handles.checkbox17,'Value') get(handles.checkbox18,'Value') get(handles.checkbox19,'Value') get(handles.checkbox20,'Value') get(handles.checkbox21,'Value') get(handles.checkbox22,'Value') get(handles.checkbox23,'Value') get(handles.checkbox24,'Value') get(handles.checkbox25,'Value') get(handles.checkbox26,'Value') get(handles.checkbox27,'Value') get(handles.checkbox28,'Value')];
and the TextLegend vector
TextLegend = (handles.LegendStrings(GetCheckboxes ==1))';
And for each checkbox callback function :
function checkbox1_Callback(hObject, eventdata, handles)
% hObject handle to checkbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% guidata(hObject, handles);
handles = guidata(gcf);
box=1;
GetCheckboxes = [get(handles.checkbox1,'Value') get(handles.checkbox2,'Value') get(handles.checkbox3,'Value') get(handles.checkbox4,'Value') get(handles.checkbox5,'Value') get(handles.checkbox6,'Value') get(handles.checkbox7,'Value') get(handles.checkbox8,'Value') get(handles.checkbox9,'Value') get(handles.checkbox10,'Value') get(handles.checkbox11,'Value') get(handles.checkbox12,'Value') get(handles.checkbox13,'Value') get(handles.checkbox14,'Value') get(handles.checkbox15,'Value') get(handles.checkbox16,'Value') get(handles.checkbox17,'Value') get(handles.checkbox18,'Value') get(handles.checkbox19,'Value') get(handles.checkbox20,'Value') get(handles.checkbox21,'Value') get(handles.checkbox22,'Value') get(handles.checkbox23,'Value') get(handles.checkbox24,'Value') get(handles.checkbox25,'Value') get(handles.checkbox26,'Value') get(handles.checkbox27,'Value') get(handles.checkbox28,'Value')];
visstates = {'off', 'on'};
thisisvis = visstates{1 + get(hObject, 'Value')};
TextLegend = (handles.LegendStrings(GetCheckboxes ==1))';
set(handles.PressurePlots{box,i}, 'Visible', thisisvis);
set(handles.TemperaturePlots{box,i}, 'Visible', thisisvis);
set(handles.PressurePlots{box,handles.SensorLocation}, 'Visible', thisisvis);
set(handles.TemperaturePlots{box,handles.SensorLocation}, 'Visible', thisisvis);
legend(handles.PressureGraph,TextLegend(:),'location','best')
legend(handles.TempGraph,TextLegend(:),'location','best')
But the problem is : -Legend are always the same color and not following the color of the plot line -It is pretty time consuming to check all the boxes at each checkbox callfunction to create the vector "GetCheckboxes"
Would you have an idea from what this would come ?
But it is updating the right value of the legend.
Thanks a lot for your help.
Marc.

