对于我正在处理的简单GUI,我需要一些帮助。
我有一个带有两个图的GUI:压力和温度以及3组数据,每组包含一条压力曲线和一条温度曲线。 我有最重要的,3复选框,使我能够选择我想要绘制的数据集。
我的问题现在是:我希望使用复选框自动更新有关我选择的数据集的图例。这意味着如果在现有复选框的顶部选中复选框,则添加新图例,但在取消选中复选框时也会删除图例。
目前,这是GUI打开时的代码:
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);
现在,对于每个复选框调用函数,我从以前的论坛获取以下代码:
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)
我正在使用该属性' DisplayName'用于存储图例字符串的每个图。我查看了不同的主题,我想有一些东西要归功于' -DynamicLegend'但无论我做什么,即使只选择了1个复选框,也会在每个情节上显示3个图例。
非常感谢任何帮助。
感谢。
编辑:
Benoit,我通过创建一个handle.LegendStrings变量来尝试你所建议的,它包含28组数据中每一组的简单字符串(每组数据包含6行):
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
端
然后创建我的GetCheckBoxes向量:
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')];
和TextLegend矢量
TextLegend = (handles.LegendStrings(GetCheckboxes ==1))';
对于每个复选框回调函数:
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')
但问题是: -Legend始终是相同的颜色,不遵循绘图线的颜色 - 检查每个复选框调用函数中的所有框以创建向量时非常耗时#34; GetCheckboxes"
你对这会是什么有所了解吗?
但它正在更新图例的正确值。
非常感谢你的帮助。
马克。
答案 0 :(得分:1)
以下是一些代码,我希望能够满足您的需求。我以编程方式创建了一个GUI,因此语法与GUIDE的结构略有不同,但想法完全相同。每个复选框都有自己的回调,当用户选择特定复选框时,将绘制相应的数据及其关联的标题和图例。您可以直接将代码应用于GUI而无需进行太多修改。实际上我没有使用' DisplayName'属性;相反,我在GUI的开头(即在您的OpeningFcn中)的单独单元格中创建了自定义图例条目(和标题),然后根据选中的复选框访问相应的条目。我觉得它更直观。
function GUI_CheckBox
clc
clear
close all
%// Create GUI components
handles.figure = figure('Position',[100 100 500 500],'Units','Pixels');
handles.axes1 = axes('Units','Pixels','Position',[60,90,400,300]);
handles.CB1 = uicontrol('Style','Checkbox','Position',[60 470 100 20],'String','Dataset 1','Value',1,'Callback',@CheckBox1Callback);
handles.CB2 = uicontrol('Style','Checkbox','Position',[60 440 100 20],'String','Dataset 2','Callback',@CheckBox2Callback);
handles.CB3 = uicontrol('Style','Checkbox','Position',[60 410 100 20],'String','Dataset 3','Callback',@CheckBox3Callback);
handles.xvalues = 1:20;
%// Create strings for legend and title for each data set.
handles.LegendStrings = {'Pressure case 1' 'Temperature case 1';'Pressure case 2' 'Temperature case 2';'Pressure case 3' 'Temperature case 3'};
handles.TitleStrings = {'Pressure & Temp case 1';'Pressure & Temp case 2';'Pressure & Temp case 3'};
handles.ColorArray = {'b' 'r';'--k' '--g';'+y' '+c'};
%// Generate dummy values
handles.PressureData = [rand(20,1) rand(20,1) rand(20,1)*.8];
handles.TempData = [rand(20,1) rand(20,1)*.9 rand(20,1)*.9];
%// Initially plot dataset 1
plot(handles.axes1,handles.xvalues,handles.TempData(:,1),handles.ColorArray{1,1})
hold on
plot(handles.axes1,handles.xvalues,handles.PressureData(:,1),handles.ColorArray{1,2})
hold off
%// Add corresponding title and legend
title(handles.TitleStrings(1));
legend(handles.LegendStrings(1,:));
guidata(handles.figure,handles);
function CheckBox1Callback(~,~)
handles = guidata(gcf);
cla %// Clear current axes
%// Get selected checkbox:
GetCheckboxes = [get(handles.CB1,'Value') get(handles.CB2,'Value') get(handles.CB3,'Value')];
for k = 1:numel(GetCheckboxes) %// If checkbox is checked, plot the data.
hold on
if GetCheckboxes(k)
plot(handles.axes1,handles.xvalues,handles.TempData(:,k),handles.ColorArray{k,1});
plot(handles.axes1,handles.xvalues,handles.PressureData(:,k),handles.ColorArray{k,2});
end
hold off
end
%// Fetch the right legend to add. Use transpose and colon operator
%to get vertical array.
TextLegend = (handles.LegendStrings(GetCheckboxes ==1,:))';
legend(TextLegend(:),'location','best')
end
function CheckBox2Callback(~,~)
handles = guidata(gcf);
cla
%// Get selected checkbox:
GetCheckboxes = [get(handles.CB1,'Value') get(handles.CB2,'Value') get(handles.CB3,'Value')];
for k = 1:numel(GetCheckboxes)
hold on
if GetCheckboxes(k)
plot(handles.axes1,handles.xvalues,handles.TempData(:,k),handles.ColorArray{k,1});
plot(handles.axes1,handles.xvalues,handles.PressureData(:,k),handles.ColorArray{k,2});
end
hold off
end
TextLegend = (handles.LegendStrings(GetCheckboxes ==1,:))';
legend(TextLegend(:),'location','best')
end
function CheckBox3Callback(~,~)
handles = guidata(gcf);
cla
%// Get selected checkbox:
GetCheckboxes = [get(handles.CB1,'Value') get(handles.CB2,'Value') get(handles.CB3,'Value')];
cla
for k = 1:numel(GetCheckboxes)
hold on
if GetCheckboxes(k)
handles.T(k) = plot(handles.axes1,handles.xvalues,handles.TempData(:,k),handles.ColorArray{k,1});
handles.P(k) = plot(handles.axes1,handles.xvalues,handles.PressureData(:,k),handles.ColorArray{k,2});
end
hold off
end
TextLegend = (handles.LegendStrings(GetCheckboxes ==1,:))';
legend(TextLegend(:),'location','best')
end
end
以下是检查框1和框3时的样子:
当选中方框2和3时:
当然,一切都是高度可定制的。希望你追求的是什么!