Matlab GUI - 使用更新功能无法更新句柄

时间:2016-03-07 14:19:32

标签: matlab matlab-guide matlab-gui

我不确定我是否以正确的方式进行此操作,但我希望有一个功能,当被调用时,基本上重置4个图。我将图存储为handles.distplot1handles.distplot2等等,希望有一个下拉菜单来选择轴上显示的图。我需要在几个不同的事件之后重置这些图,所以我很自然地想把它们放在一个函数中并避免代码冗余。我希望像

这样的功能
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);

我认为应该按预期工作,但我不知道如何调用它。在open函数中,我尝试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:open函数的问题是在打开函数setupDistPlots之前调用guidata(hObject, handles)。但是现在当我按下按钮时再次调用`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

1 个答案:

答案 0 :(得分: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使用guidata保存到GUI的setupDistPlots结构的更改将被GUIDE随后的{{1}覆盖调用。简短的回答是在 guidata之后放置setupDistPlots 以使所有内容都按照书面形式运行。

现在答案更长。我猜你很熟悉,主要是与MATLAB scripts rather than MATLAB functions合作。脚本共享基础工作区的位置functions each have their own separate workspace in memory。如上所述,guidata无法知道您已更改OpeningFcn结构,因此使用传递给handles的{​​{1}}值很高兴覆盖您的更改}。要解决这个问题,您需要为handles提供一些方法来了解您已做出更改。

setupDistPlots的一种方法是specify an output

OpeningFcn

在您的GUIDE代码中setupDistPlots来电之前放置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