0

我不确定我是否以正确的方式进行此操作,但我想要一个在调用时基本上重置 4 个图的函数。我将图存储为handles.distplot1,handles.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);

我认为它应该按预期工作,但我不知道如何称呼它。在打开功能中,我尝试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:打开函数的问题是在调用打开函数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
4

1 回答 1

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 的结构所做的更改将被 GUIDE 的后续调用覆盖。简短的回答是放在后面,以使所有内容都按所写的那样运行。setupDistPlotsguidataguidatasetupDistPlots guidata


现在更长的答案。我猜你最熟悉的是使用MATLAB 脚本而不是 MATLAB 函数。在脚本共享一个基本工作区的情况下,每个函数在内存中都有自己独立的工作区。正如所写的那样,您OpeningFcn无法知道您已经更改了结构,因此它很乐意使用传递给handles的值覆盖您的更改。为了解决这个问题,您需要包含一些方法让用户知道您已经进行了更改。handlessetupDistPlotsOpeningFcn

一种方法是为指定输出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);

在您的 GUIDE 代码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);
于 2016-03-07T15:01:18.217 回答