2016-03-07 138 views
0

我不确定我是否以正确的方式进行操作,但我希望有一个被调用的函数,基本上重置了4个图表。我将这些图存储为handles.distplot1,handles.distplot2等,希望能够下拉选择哪个图表显示在轴上。在几个不同的事件发生后,我需要重新设置这些图形,所以很自然地我想把它们抛入一个函数中,避免代码冗余。我希望像Matlab图形用户界面 - 使用更新函数更新句柄的问题

function setupDistPlots(hObject, eventdata, handles) 
    % filler data for surfc 
    x = [1 2]; 
    z = zeros(2); 
    % setup blank plots for funtion to work with 
    a = figure(1); set(a, 'Visible', 'off') 
    handles.distplot1 = surfc(x, x, z); 
    b = figure(2); set(b, 'Visible', 'off'); 
    handles.distplot2 = bar(NaN); 
    c = figure(3); set(c, 'Visible', 'off') 
    handles.distplot3 = surfc(x, x, z); 
    d = figure(4); set(d, 'Visible', 'off') 
    handles.distplot4 = bar(NaN); 
    guidata(hObject, handles); 

我认为应该按预期工作,但我不知道如何调用它。在开启功能我尝试setupDistPlots(hObject, eventdata, handles)但收到以下错误,当我尝试以后访问handles.distplot1

Reference to non-existent field 'distplot1'. 

Error in tabbedGUI>distanceToggle_Callback (line 212) 
       distribution(hObject, 
       handles.distplot1, ... 

Error in gui_mainfcn (line 95) 
     feval(varargin{:}); 

Error in tabbedGUI (line 45) 
    gui_mainfcn(gui_State, varargin{:}); 

Error in 
@(hObject,eventdata)tabbedGUI('distanceToggle_Callback',hObject,eventdata,guidata(hObject)) 


Error while evaluating UIControl Callback 

编辑:也请大家指出了所有我能得到改善。我在Matlab中做的所有事情都会让人感觉像是必须有更好的方法。

edit2:打开函数的问题在打开函数调用guidata(hObject, handles)之前调用setupDistPlots。但是现在,当我打电话`setupDistPlots再次上按下一个按钮,我得到了以下错误:

Error using matlab.graphics.primitive.Data/set 
Invalid or deleted object. 

Error in andrewDistribution (line 45) 
    set(hplot1, 'xData', x1, 'yData', y1, 'zData', zeros(length(x1))) 

Error in tabbedGUI>distanceToggle_Callback (line 200) 
       distribution(hObject, handles.distplot1, ... 

Error in gui_mainfcn (line 95) 
     feval(varargin{:}); 

Error in tabbedGUI (line 45) 
    gui_mainfcn(gui_State, varargin{:}); 

Error in @(hObject,eventdata)tabbedGUI('distanceToggle_Callback',hObject,eventdata,guidata(hObject)) 


Error while evaluating UIControl Callback 
+0

你触发该回调或MATLAB努力在加载GUI时调用该回调函数? – Suever

+0

你在开放函数中放置了哪一行?如果它在现有的'guidata(hObject,handles)'行之前,那么您的更改将被默认的GUIDE句柄覆盖。此外,它是“手柄”,而不是“手柄”。 – excaza

+0

错误是当我点击一个按钮,回调试图访问'handles.distplot1'。 'setupDistPlots'在打开的函数中运行,但似乎没有更新句柄。直到我点击某个按钮后,错误才会出现。 – Shatnerz

回答

1

我假设您最初的尝试是这样的:

% --- Executes just before testgui is made visible. 
function testgui_OpeningFcn(hObject, eventdata, handles, varargin) 
% Choose default command line output for testgui 
handles.output = hObject; 

setupDistPlots(hObject, eventdata, handles) 

% Update handles structure 
guidata(hObject, handles); 

正如交谈评论,您对handles结构保存到GUI的更改由setupDistPlots使用guidata正在被GUIDE的后续guidata调用覆盖。简短的回答是在guidata之后放setupDistPlots使所有功能按照书面形式工作。


现在回答的时间越长。我猜测你很熟悉主要与MATLAB scripts rather than MATLAB functions一起工作。脚本共享一个基本工作区,functions each have their own separate workspace in memory。如您所写,您的OpeningFcn无法知道您已更改handles结构,因此它愉快地使用传递给setupDistPlots的值handles覆盖您的更改。为了解决这个问题,你需要包含一些方法让OpeningFcn知道你已经做出了改变。

一种方法是specify an outputsetupDistPlots

function handles = setupDistPlots(hObject, eventdata, handles) 
    % filler data for surfc 
    x = [1 2]; 
    z = zeros(2); 
    % setup blank plots for funtion to work with 
    a = figure(1); set(a, 'Visible', 'off') 
    handles.distplot1 = surfc(x, x, z); 
    b = figure(2); set(b, 'Visible', 'off'); 
    handles.distplot2 = bar(NaN); 
    c = figure(3); set(c, 'Visible', 'off') 
    handles.distplot3 = surfc(x, x, z); 
    d = figure(4); set(d, 'Visible', 'off') 
    handles.distplot4 = bar(NaN); 

配售setupDistPlots在你的指导代码guidata调用之前:通过点击上的东西

% --- Executes just before testgui is made visible. 
function testgui_OpeningFcn(hObject, eventdata, handles, varargin) 
% Choose default command line output for testgui 
handles.output = hObject; 

handles = setupDistPlots(hObject, eventdata, handles); 

% Update handles structure 
guidata(hObject, handles); 
+0

谢谢。我更习惯于处理函数。我对Matlab更新,不知道'guidata'如何工作。我认为它更新了全球或类似的东西,但你所说的话是非常有道理的。我经常忘记'handles'作为参数传递。我觉得自己像个白痴。我也编辑了我的问题。我会看看这些建议是否有帮助。 – Shatnerz