Matlab GUI - 使用更新函数更新句柄时出现问题
Matlab GUI - Trouble updating handles using an update function
我不确定我这样做是否正确,但我希望有一个函数,在调用时基本上会重置 4 个绘图。我将图存储为 handles.distplot1
、handles.distplot2
等,希望下拉到 select 轴上显示的图。我需要在几个不同的事件之后重置这些图,所以我自然想把它们放在一个函数中并避免代码冗余。我希望像
这样的功能
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);
如评论中所述,您对 setupDistPlots
使用 guidata
保存到 GUI 的 handles
结构所做的更改正在被 GUIDE 的后续 guidata
覆盖称呼。简短的回答是将 setupDistPlots
放在 guidata
之后,以使所有内容都按书面形式运行。
现在更长的答案。我猜你对 MATLAB scripts rather than MATLAB functions. Where scripts share a base workspace, functions each have their own separate workspace in memory 的工作很熟悉。如所写,您的 OpeningFcn
无法知道您已经更改了 handles
结构,因此它很乐意使用传递给 setupDistPlots
的 handles
的值覆盖您的更改].为了解决这个问题,您需要包含一些方法让 OpeningFcn
知道您已经进行了更改。
一种方法是 specify an output for 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);
我不确定我这样做是否正确,但我希望有一个函数,在调用时基本上会重置 4 个绘图。我将图存储为 handles.distplot1
、handles.distplot2
等,希望下拉到 select 轴上显示的图。我需要在几个不同的事件之后重置这些图,所以我自然想把它们放在一个函数中并避免代码冗余。我希望像
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);
如评论中所述,您对 setupDistPlots
使用 guidata
保存到 GUI 的 handles
结构所做的更改正在被 GUIDE 的后续 guidata
覆盖称呼。简短的回答是将 setupDistPlots
放在 guidata
之后,以使所有内容都按书面形式运行。
现在更长的答案。我猜你对 MATLAB scripts rather than MATLAB functions. Where scripts share a base workspace, functions each have their own separate workspace in memory 的工作很熟悉。如所写,您的 OpeningFcn
无法知道您已经更改了 handles
结构,因此它很乐意使用传递给 setupDistPlots
的 handles
的值覆盖您的更改].为了解决这个问题,您需要包含一些方法让 OpeningFcn
知道您已经进行了更改。
一种方法是 specify an output for 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);