2014-11-14 3 views
0

간단한 GUI에 관해 도움이 필요합니다.GUI에서 선택 상자를 사용하여 선택 가능한 플롯의 동적 범례

저는 압력과 온도의 두 가지 그래프와 각각 하나의 압력 곡선과 하나의 온도 곡선이 포함 된 3 세트의 데이터가있는 GUI를 가지고 있습니다. 필자가 그려야 할 데이터 세트를 선택할 수있는 3 개의 확인란이 있습니다.

내 질문은 다음과 같습니다. 확인란을 사용하여 선택한 데이터 집합에 대한 범례가 자동으로 업데이트되기를 원합니다. 즉, 기존 범례 위에 확인란이 선택되어 있으면 확인란을 선택 취소하고 범례를 제거하면 새 범례가 추가됩니다.

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 : 나는 다음과 같은 코드가 이전 포럼에서 촬영 한 각 확인란 통화 기능 지금

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); 

을 그리고 :

현재이 GUI를 열 때 나는 코드로 무엇을 가지고 범례 문자열을 저장할 각 플롯에 'DisplayName'속성을 사용하고 있습니다. 나는 다른 주제를 쳐다 보았고 '-DynamicLegend'덕분에 그 자리에 놓을 게 있다고 생각했지만, 내가하는 일과 상관없이, 단 하나의 체크 박스 만 선택되었다고해도 각 플롯에 3 개의 범례가 항상 표시됩니다.

도움이 될 것입니다.

감사합니다.


편집 :

Benoit 씨, 난 당신이 데이터의 28 개 세트 각각에 대한 간단한 문자열을 포함하는 handles.LegendStrings 변수 (각 데이터 세트를 작성하여 제시 한 내용을 시도한 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 벡터 생성 :

,536을
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')]; 

91,363,210와 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 항상 같은 색상의 색을 다음 없습니다 플롯 라인 - "checkCheckboxes"벡터를 만들기 위해 각 체크 박스 callfunction에서 모든 박스를 체크하는 것은 꽤 시간이 걸립니다.

이것이 어떻게 될지 아이디어가 있습니까?

그러나 범례의 올바른 값을 업데이트 중입니다.

도움을 주셔서 감사합니다.

마크.

+0

안녕하세요. Marc 님의 답변을 통해 문제를 해결할 수 있었습니까? –

+0

안녕하세요 Benoit. 귀하의 답변 아래 내 의견을 읽으십시오. 고마워. – Marc

+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이 점검 때 모습입니다 :

enter image description here

그리고 상자 2와 3을 확인하는 경우 :

enter image description here

모두가 물론 사용자 정의가 가능합니다. 그것이 당신이 쫓았 던 것이기를 바랍니다!

+0

안녕하세요 Benoit, 귀하의 도움과 지원에 많은 감사를드립니다! 내가 충분히 설명하지 못했을 수도있는 한 가지 점이 있습니다. 같은 시간에 두 개 이상의 데이터 집합을 그릴 수 있기를 바랍니다. 코드에서 하나를 선택하면 모든 체크 상자의 선택을 취소합니다. 여러 개의 데이터 세트 (예 : 여러 개의 확인란 선택)를 가질 가능성이있는 범례가 필요합니다. 이미 제공하신 지원에 대해 다시 한 번 감사드립니다. – Marc

관련 문제