MATLAB Answers

Multiple plot controlled by the same popupmenu in GUI

조회 수: 7(최근 30일)
JB
JB 3 Sep 2017
I have this code, where I have a popupmenu for the user to decide the type of plot to be displayed in axes 1 and 2. The plots should only be visible if the checkmark is activated. If the checkmark is activated the plot should update if the popupmenu value is changed. So far it is working fine for the first plot. But I get a error code: "Function with duplicate name "myPlotFcn" cannot be defined." Do I have to define a new "myPlotFcn" for every plot, or is there a smoother way to control multiple (~50) plot from the same popupmenu? Here is my code:
% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
myPlotFcn(handles);
function popupmenu1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in checkbox1.
function checkbox1_Callback(hObject, eventdata, handles)
myPlotFcn(handles);
function myPlotFcn(handles)
isChecked = get(handles.checkbox1,'value');
if(isChecked)
contents = get(handles.popupmenu1,'String');
popupmenu1value = contents{get(handles.popupmenu1,'Value')};
switch popupmenu1value
case 'Raw CD [mdeg]'
rawdata=handles.rawdata;
x1=rawdata{1,2}(:,1);
cd1raw=(rawdata{1,2}(:,2)-rawdata{1,1}(:,2));%sample-buffer
cd1raw=cd1raw-cd1raw(1,1); %normalize to y=0 at 250 nm
CD=cd1raw;
ht1=rawdata{1,2}(:,3);
%hold( handles.axes1, 'on' )
handles.plotCD1 = plot(x1,CD,'LineWidth',2,'Color', [0 0 0],'parent',handles.axes1);
%hold( handles.axes2, 'on' )
handles.plotHT1 = plot(x1,ht1,'LineWidth',2,'Color', [0 0 0],'parent',handles.axes2);
guidata(handles.plotCD1,handles);
guidata(handles.plotHT1,handles);
case 'Molar ellipticity [deg cm^2 dmol^-1]'
mg_ml_Conc=handles.mg_ml_Conc;
Length=handles.Length;
Mass=handles.Mass;
rawdata=handles.rawdata;
x1=rawdata{1,2}(:,1);
cd1raw=(rawdata{1,2}(:,2)-rawdata{1,1}(:,2));%sample-buffer
cd1raw=cd1raw-cd1raw(1,1); %normalize to y=0 at 250 nm
CD=((cd1raw*Mass(1,1))/(10*Length(1,1)*mg_ml_Conc(1,1)));
%molar ellipticity=cdraw*mw/(10*cell-length(cm)*concentration(mg/ml))
ht1=rawdata{1,2}(:,3);
%hold( handles.axes1, 'on' )
handles.plotCD1 = plot(x1,CD,'LineWidth',2,'Color', [0 0 0],'parent',handles.axes1);
%hold( handles.axes2, 'on' )
handles.plotHT1 = plot(x1,ht1,'LineWidth',2,'Color', [0 0 0],'parent',handles.axes2);
guidata(handles.plotCD1,handles);
guidata(handles.plotHT1,handles);
case 'Mean residue ellipticity [deg cm^2 dmol^-1 residue^-1]'
Length=handles.Length;
mg_ml_Conc=handles.mg_ml_Conc;
Mass=handles.Mass;
Peptide=handles.Peptide;
rawdata=handles.rawdata;
x1=rawdata{1,2}(:,1);
cd1raw=(rawdata{1,2}(:,2)-rawdata{1,1}(:,2));%sample-buffer
cd1raw=cd1raw-cd1raw(1,1); %normalize to y=0 at 250 nm
CD=(cd1raw/1000*100)/(Length(1,1)*mg_ml_Conc(1,1)/Mass(1,1)*Peptide(1,1));
%Normalized data:(abscorrected/1000*100)/(pathlength*concentration(mg/ml)/Mw*no. of peptidebonds)
ht1=rawdata{1,2}(:,3);
%hold( handles.axes1, 'on' )
handles.plotCD1 = plot(x1,CD,'LineWidth',2,'Color', [0 0 0],'parent',handles.axes1);
%hold( handles.axes2, 'on' )
handles.plotHT1 = plot(x1,ht1,'LineWidth',2,'Color', [0 0 0],'parent',handles.axes2);
guidata(handles.plotCD1,handles);
guidata(handles.plotHT1,handles);
end
else
if ~isempty(handles.plotCD1)
delete(handles.plotCD1)
~isempty(handles.plotHT1)
delete(handles.plotHT1)
set(handles.text2, 'BackgroundColor', [0.94 0.94 0.94]);
end
end
% --- Executes on button press in checkbox3.
function checkbox3_Callback(hObject, eventdata, handles)
myPlotFcn(handles);
function myPlotFcn(handles)
isChecked = get(handles.checkbox3,'value');
if(isChecked)
contents = get(handles.popupmenu1,'String');
popupmenu1value = contents{get(handles.popupmenu1,'Value')};
switch popupmenu1value
case 'Raw CD [mdeg]'
rawdata=handles.rawdata;
x1=rawdata{1,2}(:,1);
cd1raw=(rawdata{1,2}(:,2)-rawdata{1,1}(:,2));%sample-buffer
cd1raw=cd1raw-cd1raw(1,1); %normalize to y=0 at 250 nm
CD=cd1raw;
ht1=rawdata{1,2}(:,3);
%hold( handles.axes1, 'on' )
handles.plotCD1Adj = plot(x1,CD,'LineWidth',2,'Color', [0 0 0],'parent',handles.axes1);
%hold( handles.axes2, 'on' )
handles.plotHT1Adj = plot(x1,ht1,'LineWidth',2,'Color', [0 0 0],'parent',handles.axes2);
guidata(handles.plotCD1Adj,handles);
guidata(handles.plotHT1Adj,handles);
case 'Molar ellipticity [deg cm^2 dmol^-1]'
... and so on similar to the above checkbox, but with different x,y values
I guess the code is a bit clumsy so any suggestion for a more clean code which will contain 50 checkboxes is highly appreciated. THANKS.

  댓글 수: 0

로그인 to comment.

채택된 답변

Cam Salzberger
Cam Salzberger 3 Sep 2017
Hey JB,
I'd highly suggest having only one function that actually does the work. Just change the way you call that function so that it checks the correct checkbox, and manipulates the correct plot.
I personally would just create each checkbox programmatically (if you really have 50 of them) and assign them all to call that same function, but change what the call would look like. Something like:
chkbxs = gobjects(50,1); % Preallocate checkbox array
xFirst = 10; % Define where you want these checkboxes positioned
yFirst = 510;
yGap = 10;
bxWidth = 50;
bxHeight = 10;
for k = 1:50
pos = [xFirst yFirst-yGap*(k-1) bxWidth bxHeight];
chkbxs(k) = uicontrol(...
'Style','checkbox',...
'Position',pos,...
'Callback',@(~,~) updatePlots(k));
end
handles.checkboxes = chkbxs;
guidata(handles)
Now in the "updatePlots" function, you can get the handles with guidata, check whether the checkbox changed is on or off, and update the respective plot accordingly.
-Cam

  댓글 수: 0

로그인 to comment.

More Answers (0)

이 질문에 답변하려면 로그인을(를) 수행하십시오.

제품


Translated by