多个绘图由GUI

时间:2017-09-03 08:31:05

标签: matlab matlab-guide

我有这个代码,我有一个弹出菜单供用户决定要在轴1和2中显示的绘图类型。只有勾选了勾选标记才能看到绘图。如果勾选了复选标记,则如果弹出菜单值已更改,则应更新绘图。到目前为止,第一个情节的工作正常。但我得到一个错误代码:"具有重复名称的功能" myPlotFcn"无法定义。"我是否必须定义一个新的" myPlotFcn"对于每个情节,还是有一种更平滑的方式来控制来自同一弹出菜单的多个(~50)情节?这是我的代码:

% --- 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

我猜这段代码有点笨拙,所以对于包含50个复选框的更干净的代码的任何建议都非常感激。感谢。

1 个答案:

答案 0 :(得分:0)

除了几个值之外,你需要myPlotFcn的每个“版本”或多或少相同的代码,并且 - 在你的例子中 - 要检查的复选框变量,然后将此函数写入ONCE。 (Matlab也会更开心,因为它会知道你想要调用哪个函数!)

使用myPlotFcn的其他输入变量来区分调用 - 复选框的字符串,以获取代码所需的任何x,y值。这将涉及对现有代码进行一些重写以接受当前存在硬值的变量,但它将产生更清晰的代码,可以比目前为止的方法更简单地扩展到许多复选框。

每个复选框回调函数只需要使用正确的附加变量集调用此单个myPlotFcn。