如何在 MATLAB 的函数内创建 GUI? [英] How to create a GUI inside a function in MATLAB?
问题描述
是否可以从函数内部编写 GUI?
Is it possible to write a GUI from inside a function?
问题是所有 GUI 函数的回调都在全局工作区中进行评估.但是函数有自己的工作空间,不能访问全局工作空间中的变量.是否可以使 GUI 功能使用该功能的工作区?例如:
The problem is that the callback of all GUI-functions are evaluated in the global workspace. But functions have their own workspace and can not access variables in the global workspace. Is it possible to make the GUI-functions use the workspace of the function? For example:
function myvar = myfunc()
myvar = true;
h_fig = figure;
% create a useless button
uicontrol( h_fig, 'style', 'pushbutton', ...
'string', 'clickme', ...
'callback', 'myvar = false' );
% wait for the button to be pressed
while myvar
pause( 0.2 );
end
close( h_fig );
disp( 'this will never be displayed' );
end
此事件循环将无限期运行,因为回调不会修改函数中的 myvar
.相反,它将在全局工作区中创建一个新的 myvar
.
This event-loop will run indefinitely, since the callback will not modify myvar
in the function. Instead it will create a new myvar
in the global workspace.
推荐答案
构建 GUI,例如使用 App Designer、GUIDE 或以编程方式创建它(我将在下面说明此选项).了解 为您的 GUI 组件和 可用于在组件之间共享数据的选项.
There are a number of ways to build a GUI, such as using the App Designer, GUIDE, or creating it programmatically (I'll illustrate this option below). It's also important to be aware of the different ways to define callback functions for your GUI components and the options available for sharing data between components.
我偏爱的方法是使用嵌套函数 作为回调.下面以一个简单的 GUI 为例:
The approach I'm partial to is using nested functions as callbacks. Here's a simple GUI as an example:
function make_useless_button()
% Initialize variables and graphics:
iCounter = 0;
hFigure = figure;
hButton = uicontrol('Style', 'pushbutton', 'Parent', hFigure, ...
'String', 'Blah', 'Callback', @increment);
% Nested callback function:
function increment(~, ~)
iCounter = iCounter+1;
disp(iCounter);
end
end
当你运行这段代码时,每次按下按钮,显示的计数器应该加一,因为嵌套函数increment
可以访问make_useless_button
的工作区,并且从而可以修改iCounter
.请注意,按钮回调设置为 函数句柄increment
,并且这个函数默认必须接受两个参数:触发回调的UI组件的图形句柄,以及关联事件数据的结构.我们使用~
忽略它们 在这种情况下,因为我们没有使用它们.
When you run this code, the counter displayed should increment by one every time you press the button, because the nested function increment
has access to the workspace of make_useless_button
and thus can modify iCounter
. Note that the button callback is set to a function handle to increment
, and that this function must accept two arguments by default: a graphics handle for the UI component that triggered the callback, and a structure of associated event data. We ignore them with the ~
in this case since we aren't using them.
将上述方法扩展到您的特定问题,您可以添加循环并更改回调,以便将您的标志变量设置为 false:
Extending the above approach to your particular problem, you could add your loop and change the callback so it sets your flag variable to false:
function make_stop_button()
% Initialize variables and graphics:
keepLooping = true;
hFigure = figure;
hButton = uicontrol('Style', 'pushbutton', 'Parent', hFigure, ...
'String', 'Stop', 'Callback', @stop_fcn);
% Keep looping until the button is pressed:
while keepLooping,
drawnow;
end
% Delete the figure:
delete(hFigure);
% Nested callback function:
function stop_fcn(~, ~)
keepLooping = false;
end
end
需要 drawnow
这里给按钮回调一个机会来中断循环内的程序流程并修改keepLooping
的值.
这篇关于如何在 MATLAB 的函数内创建 GUI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!