我不确定我是否以正确的方式做到这一点,但我想有一个函数,当调用时,基本上重置 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:打开函数的问题是在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
我假设你最初的尝试看起来像这样:
% --- 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);
如注释中所述,您对通过使用guidata
setupDistPlots
保存到 GUI 的handles
结构所做的更改将被 GUIDE 的后续guidata
调用覆盖。简短的回答是将setupDistPlots
放在guidata
之后,以使所有内容都按书面形式运行。
现在更长的答案。我猜你熟悉的主要是使用 MATLAB 脚本而不是 MATLAB 函数。在脚本共享基本工作区的情况下,每个函数在内存中都有自己单独的工作区。如前所述,您的OpeningFcn
无法知道您已经更改了handles
结构,因此它很乐意使用传递给setupDistPlots
的handles
值覆盖您的更改。要解决此问题,您需要包含一些方法,以使OpeningFcn
知道您进行了更改。
一种方法是为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 代码中guidata
调用之前放置setupDistPlots
:
% --- 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);