2017-09-03 43 views
0

我有这个代码,其中有一个弹出菜单供用户决定要在轴1和2中显示的绘图类型。只有当复选标记为活性。如果复选标记已激活,则如果popupmenu值发生更改,则绘图应该更新。到目前为止,它对第一个情节工作良好。但是我得到一个错误代码:“具有重复名称的函数”myPlotFcn“无法定义。”我是否必须为每个情节定义一个新的“myPlotFcn”,还是有更平滑的方法来控制同一个popupmenu中的多个(〜50)情节?这里是我的代码:通过GUI中的相同弹出菜单控制的多个绘图

% --- 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个复选框任何建议高度赞赏。谢谢。

+0

Matlab是正确的...为什么你使用不同的功能相同的名称?如何调试它,例如...?你为什么不“结束”你的功能?这真是令人困惑。 – Adiel

回答

0

对于myPlotFcn的每个'版本',除了几个值之外,还有 - 在您的示例中 - 需要检查的复选框变量,您需要的代码大致相同,然后将此函数写入ONCE。 (Matlab也会更快乐,因为它会知道你想调用哪个函数!)

使用额外的输入变量myPlotFcn来区分调用 - 一个字符串的复选框,以获得值的任何x,y值代码将需要。这将涉及到一些现有代码的重写,以接受当前存在硬值的变量,但它会产生更简洁的代码,以比迄今为止的方法更简单的方式扩展到许多复选框。

每个复选框回调函数然后只需要调用这个单独的myPlotFcn和正确的一组附加变量。

+0

嘿etmuse。非常感谢您的回答。对不起,我在matlab编码方面很新颖。通过“复选框变量进行检查,然后写这个函数只是一次”,你是指所有的复选框值作为一个参数,然后根据循环函数中的值绘图? –

+0

写出一个名为myPlotFcn的函数,就像你已经完成的那样。而不是在x,y的值和要查看的复选框的名称中进行硬编码,而不是使用变量来编写它们。 (取决于复选框的任何其他值都会相同) 因此,您最终将使用myPlotFcn(句柄,checkboxname,x,y)作为您的函数。 在每个回调函数(例如checkbox1_Callback,checkbox3_Callback如上所示)中,然后调用myPlotFcn(句柄,'checkbox1',3,5)或其他。 – etmuse

+0

嗨etmuse。首先非常感谢花费时间来帮助我。然而,我没有matlab知识来执行你的建议,这是一个耻辱,因为它听起来很酷。但是,如果您有兴趣,我尝试了另一种方法:https://stackoverflow.com/questions/46054269/display-selected-plots-in-gui-axes –

相关问题