如何在图形用户界面中重置默认值
How to reset the default values in a graphical user interface
我正在(手动)创建一个图形界面,我想要一个重置默认值的重置按钮。
我已经编码了
H.but(3) = uicontrol('Units','normalized', ...
'BackgroundColor',[1 0.7 1], ...
'Callback','MyFunction(''Reset'');', ...
'FontSize',12, ...
'Position',[0.04 0.54 0.1 0.05 ], ...
'String','Reset');
case 'Reset'
clear all % Is not working and I think that isn't that I expect
set(findobj(H.fig,'style','edit', '-or','style','text'),'string','') % H is a global variable. This trial don't give the default value, it just clear certain boxes
如果您真的想从头开始重新启动 GUI,最简单的方法就是关闭它再打开它。您可以从按钮的回调中做到这一点。我指出了一个新函数 restartGUI
。这可能是您的主要 gui 的子功能或它自己的 m 文件,您可以选择。
你的问题在细节上很简单,所以我无法帮助你解决一些细节问题,但这应该能让你大致了解一下。
如果关闭和打开不是您想要的,那么在 restartGUI
函数中您将不得不手动重置每个 uicontrol 等的状态(无论您的 GUI 中还有什么我们看不到)。
H.but(3) = uicontrol('Units','normalized', ...
'BackgroundColor',[1 0.7 1], ...
'Callback',@restartGUI, ...
'FontSize',12, ...
'Position',[0.04 0.54 0.1 0.05 ], ...
'String','Reset');
% <<<< THE rest of your code >>>
function restartGUI(hObject,varargin)
global H
close(H.fig) %Assuming H.fig the main GUI window.
%Call the GUI again which will restart it.
yourGUIFunction
编辑:添加了使用全局 H 来关闭。
我通常更喜欢为我的 GUI 创建一个特定的 reset_gui
函数来重置所有相关的控件属性(例如复选框状态、可编辑文本框中的字符串等)以适合默认值,以及将所有相关变量值设置为默认值、清除绘图等。
如果您更喜欢将所有 UI 控件属性重置为初始状态的通用选项,下面是一个可能的解决方案示例:
function example_reset_gui
% Initialize GUI:
hFigure = figure();
uicontrol('Style', 'edit', 'Position', [20 100 100 25]);
uicontrol('Style', 'edit', 'Position', [20 65 100 25]);
uicontrol('Style', 'push', 'Position', [20 20 60 30], ...
'String', 'Reset', 'Callback', @reset_fcn);
drawnow
% Collect default states:
[defaultState{1:3}] = get_default_state(hFigure);
% Nested reset function:
function reset_fcn(~, ~)
set(defaultState{:});
end
end
% Local function:
function [hArray, propArray, valueArray] = get_default_state(hFigure)
hArray = findall(hFigure, 'Type', 'uicontrol');
propArray = fieldnames(set(hArray(1)));
valueArray = get(hArray, propArray);
end
这将创建一个带有 2 个可编辑文本框和一个重置按钮的图形。您可以在文本框中键入任何内容,当您按下重置按钮时,它们将被清除(即将它们设置为它们最初包含的默认空字符串)。
local function get_default_state
will find all the uicontrol
objects in the figure, then get all of their set
-able properties (i.e. all properties that are not read-only), then get all the initial values for those properties. The three outputs are stored in the 1-by-3 cell array defaultState
, which is accessible by the nested function reset_fcn
。当按下重置按钮时,所有 set
-able UI 控件属性都设置为它们在首次创建时的值。
需要注意的是,对 Position
property (such as due to resizing of the figure) could be undone by this approach. Using 'normalized'
units 所做的任何更改都会避免这种情况。
我正在(手动)创建一个图形界面,我想要一个重置默认值的重置按钮。
我已经编码了
H.but(3) = uicontrol('Units','normalized', ...
'BackgroundColor',[1 0.7 1], ...
'Callback','MyFunction(''Reset'');', ...
'FontSize',12, ...
'Position',[0.04 0.54 0.1 0.05 ], ...
'String','Reset');
case 'Reset'
clear all % Is not working and I think that isn't that I expect
set(findobj(H.fig,'style','edit', '-or','style','text'),'string','') % H is a global variable. This trial don't give the default value, it just clear certain boxes
如果您真的想从头开始重新启动 GUI,最简单的方法就是关闭它再打开它。您可以从按钮的回调中做到这一点。我指出了一个新函数 restartGUI
。这可能是您的主要 gui 的子功能或它自己的 m 文件,您可以选择。
你的问题在细节上很简单,所以我无法帮助你解决一些细节问题,但这应该能让你大致了解一下。
如果关闭和打开不是您想要的,那么在 restartGUI
函数中您将不得不手动重置每个 uicontrol 等的状态(无论您的 GUI 中还有什么我们看不到)。
H.but(3) = uicontrol('Units','normalized', ...
'BackgroundColor',[1 0.7 1], ...
'Callback',@restartGUI, ...
'FontSize',12, ...
'Position',[0.04 0.54 0.1 0.05 ], ...
'String','Reset');
% <<<< THE rest of your code >>>
function restartGUI(hObject,varargin)
global H
close(H.fig) %Assuming H.fig the main GUI window.
%Call the GUI again which will restart it.
yourGUIFunction
编辑:添加了使用全局 H 来关闭。
我通常更喜欢为我的 GUI 创建一个特定的 reset_gui
函数来重置所有相关的控件属性(例如复选框状态、可编辑文本框中的字符串等)以适合默认值,以及将所有相关变量值设置为默认值、清除绘图等。
如果您更喜欢将所有 UI 控件属性重置为初始状态的通用选项,下面是一个可能的解决方案示例:
function example_reset_gui
% Initialize GUI:
hFigure = figure();
uicontrol('Style', 'edit', 'Position', [20 100 100 25]);
uicontrol('Style', 'edit', 'Position', [20 65 100 25]);
uicontrol('Style', 'push', 'Position', [20 20 60 30], ...
'String', 'Reset', 'Callback', @reset_fcn);
drawnow
% Collect default states:
[defaultState{1:3}] = get_default_state(hFigure);
% Nested reset function:
function reset_fcn(~, ~)
set(defaultState{:});
end
end
% Local function:
function [hArray, propArray, valueArray] = get_default_state(hFigure)
hArray = findall(hFigure, 'Type', 'uicontrol');
propArray = fieldnames(set(hArray(1)));
valueArray = get(hArray, propArray);
end
这将创建一个带有 2 个可编辑文本框和一个重置按钮的图形。您可以在文本框中键入任何内容,当您按下重置按钮时,它们将被清除(即将它们设置为它们最初包含的默认空字符串)。
local function get_default_state
will find all the uicontrol
objects in the figure, then get all of their set
-able properties (i.e. all properties that are not read-only), then get all the initial values for those properties. The three outputs are stored in the 1-by-3 cell array defaultState
, which is accessible by the nested function reset_fcn
。当按下重置按钮时,所有 set
-able UI 控件属性都设置为它们在首次创建时的值。
需要注意的是,对 Position
property (such as due to resizing of the figure) could be undone by this approach. Using 'normalized'
units 所做的任何更改都会避免这种情况。