如何在 GUI 轴句柄中显示浮动图形
how to display a floating figure in a GUI axes handle
我想在 Matlab 的 GUI 中显示当前时间,我想使用此 link 中的教程 (GUI_17) 来制作手表(或类似的东西)。代码如下:
function [] = GUI_17()
% Demonstrate how to have a running clock in a GUI, and timer use.
% Creates a small little GUI which displays the correct time and is updated
% every minute according to the system clock.
%
% Author: Matt Fig
% Date: 1/15/2010
S.fh = figure('units','pixels',...
'position',[300 300 180 50],...
'menubar','none',...
'name','GUI_17',...
'numbertitle','off',...
'resize','off');
S.tx = uicontrol('style','text',...
'unit','pix',...
'position',[35 10 110 30],...
'string',datestr(now,16),...
'backgroundc',get(S.fh,'color'),...
'fontsize',18,...
'fontweight','bold',...
'foregroundcolor',[.9 .1 .1]);
STRT = 60 - str2double(datestr(now,'ss')); % So we can update every minute.
tmr = timer('Name','Reminder',...
'Period',60,... % Update the time every 60 seconds.
'StartDelay',STRT,... % In seconds.
'TasksToExecute',inf,... % number of times to update
'ExecutionMode','fixedSpacing',...
'TimerFcn',{@updater});
start(tmr); % Start the timer object.
set(S.fh,'deletefcn',{@deleter}) % Kill timer if fig is closed.
function [] = updater(varargin)
% timerfcn for the timer. If figure is deleted, so is timer.
% I use a try-catch here because timers are finicky in my
% experience.
try
set(S.tx,'string',datestr(now,16))
if ~str2double(datestr(now,'MM'))
X = load('gong'); % At the hour, sound a gong.
sound(X.y,X.Fs*2.5)
end
clear X
catch
delete(S.fh) % Close it all down.
end
end
function [] = deleter(varargin)
% If figure is deleted, so is timer.
stop(tmr);
delete(tmr);
end
end
我使用 GUIDE 创建了自己的 GUI,并创建了一个空轴 1,我想用它来替换原始时钟函数中的图形定义。我从 link 复制了整个函数并将其添加到我的 GUIDE 程序的底部。这是我尝试替换函数的图形句柄:
% S.fh = figure('units','pixels',...
% 'position',[300 300 180 50],...
% 'menubar','none',...
% 'name','GUI_17',...
% 'numbertitle','off',...
% 'resize','off');
通过以下方式:
S.fh=handles.axes1;
但是它没有在框架中显示任何输出,正确的做法是什么?
谢谢
如果你想使用 GUIDE
工具精确复制教程的 GUI,首先你必须注意到在教程中时钟是使用 text
uicontrol 创建的,所以它很清楚为什么要在 GUI 中插入 axes
.
还有。 GUI "window" (figure
) 是由 GUIDE 自动创建的,所以不需要添加轴,除非你想创建一个 "analog" 带有时针、分针的时钟等,但是,在这种情况下,GUI 将变得更加复杂。
使用GUIDE
你必须简单地添加一个text uicontrol
,然后,如果你想精确地重现教程中时钟的形状,你可以设置figure
和 text
uicontrol 通过 Property Inspector
.
创建 GUI 后,您可以在其 .m
文件中插入教程中的函数,以使其正常工作,如下所示:
- 在 GUI 中插入用于创建计时器及其初始化的代码
OpeningFcn
- 使用图中的
handles
在figure uicontrols
之间共享变量
- 添加图形的 handel 作为
updater
和 deleter
函数的附加输入参数
然后您只需复制并粘贴 updater
和 deleter
函数,varargin
cellarray 的第三个元素将是您在其中设置的图形句柄结构,在OpeningFcn
timer
数据。
关于教程中的代码,您必须在 updater
函数中添加以下行:
fig_handles=varargin{3}
检索 text
uicontrol 的句柄。
使用 GUIDE 创建的整个 GUI 如下所示(代码中的注释应突出显示上述相关修改):
function varargout = GUI_1_GUIDE(varargin)
% GUI_1_GUIDE MATLAB code for GUI_1_GUIDE.fig
% GUI_1_GUIDE, by itself, creates a new GUI_1_GUIDE or raises the existing
% singleton*.
%
% H = GUI_1_GUIDE returns the handle to a new GUI_1_GUIDE or the handle to
% the existing singleton*.
%
% GUI_1_GUIDE('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GUI_1_GUIDE.M with the given input arguments.
%
% GUI_1_GUIDE('Property','Value',...) creates a new GUI_1_GUIDE or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before GUI_1_GUIDE_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to GUI_1_GUIDE_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help GUI_1_GUIDE
% Last Modified by GUIDE v2.5 16-Aug-2018 18:29:37
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @GUI_1_GUIDE_OpeningFcn, ...
'gui_OutputFcn', @GUI_1_GUIDE_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before GUI_1_GUIDE is made visible.
function GUI_1_GUIDE_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to GUI_1_GUIDE (see VARARGIN)
% Choose default command line output for GUI_1_GUIDE
handles.output = hObject;
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Insert here the code for the creation of the timer and its initialization
% Use the figure's handles to share the variables among the figure
% uicontrols
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
handles.text_clock.String=datestr(now,16);
handles.STRT = 60 - str2double(datestr(now,'ss')); % So we can update every minute.
handles.tmr = timer('Name','Reminder',...
'Period',60,... % Update the time every 60 seconds.
'StartDelay',handles.STRT,... % In seconds.
'TasksToExecute',inf,... % number of times to update
'ExecutionMode','fixedSpacing')
set(handles.tmr,'TimerFcn',{@updater handles});
%
% Add the figure's handels as additional input parameter of the "updater"
% and "deleter" function
%
set(hObject,'deletefcn',{@deleter handles}) % Kill timer if fig is closed.
start(handles.tmr); % Start the timer object.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes GUI_1_GUIDE wait for user response (see UIRESUME)
% uiwait(handles.figure1);
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Add the "updater" and "deleter" function
% The third element of the "varargin" parameter is the figure's handle
% struct
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
function [] = updater(varargin)
% timerfcn for the timer. If figure is deleted, so is timer.
% I use a try-catch here because timers are finicky in my
% experience.
%
fig_handles=varargin{3}
try
set(fig_handles.text_clock,'string',datestr(now,16))
if ~str2double(datestr(now,'MM'))
X = load('gong'); % At the hour, sound a gong.
sound(X.y,X.Fs*2.5)
end
clear X
catch
delete(fig_handles.figure1) % Close it all down.
end
function [] = deleter(varargin)
% If figure is deleted, so is timer.
fig_handles=varargin{3}
stop(fig_handles.tmr);
delete(fig_handles.tmr);
% --- Outputs from this function are returned to the command line.
function varargout = GUI_1_GUIDE_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
我想在 Matlab 的 GUI 中显示当前时间,我想使用此 link 中的教程 (GUI_17) 来制作手表(或类似的东西)。代码如下:
function [] = GUI_17()
% Demonstrate how to have a running clock in a GUI, and timer use.
% Creates a small little GUI which displays the correct time and is updated
% every minute according to the system clock.
%
% Author: Matt Fig
% Date: 1/15/2010
S.fh = figure('units','pixels',...
'position',[300 300 180 50],...
'menubar','none',...
'name','GUI_17',...
'numbertitle','off',...
'resize','off');
S.tx = uicontrol('style','text',...
'unit','pix',...
'position',[35 10 110 30],...
'string',datestr(now,16),...
'backgroundc',get(S.fh,'color'),...
'fontsize',18,...
'fontweight','bold',...
'foregroundcolor',[.9 .1 .1]);
STRT = 60 - str2double(datestr(now,'ss')); % So we can update every minute.
tmr = timer('Name','Reminder',...
'Period',60,... % Update the time every 60 seconds.
'StartDelay',STRT,... % In seconds.
'TasksToExecute',inf,... % number of times to update
'ExecutionMode','fixedSpacing',...
'TimerFcn',{@updater});
start(tmr); % Start the timer object.
set(S.fh,'deletefcn',{@deleter}) % Kill timer if fig is closed.
function [] = updater(varargin)
% timerfcn for the timer. If figure is deleted, so is timer.
% I use a try-catch here because timers are finicky in my
% experience.
try
set(S.tx,'string',datestr(now,16))
if ~str2double(datestr(now,'MM'))
X = load('gong'); % At the hour, sound a gong.
sound(X.y,X.Fs*2.5)
end
clear X
catch
delete(S.fh) % Close it all down.
end
end
function [] = deleter(varargin)
% If figure is deleted, so is timer.
stop(tmr);
delete(tmr);
end
end
我使用 GUIDE 创建了自己的 GUI,并创建了一个空轴 1,我想用它来替换原始时钟函数中的图形定义。我从 link 复制了整个函数并将其添加到我的 GUIDE 程序的底部。这是我尝试替换函数的图形句柄:
% S.fh = figure('units','pixels',...
% 'position',[300 300 180 50],...
% 'menubar','none',...
% 'name','GUI_17',...
% 'numbertitle','off',...
% 'resize','off');
通过以下方式:
S.fh=handles.axes1;
但是它没有在框架中显示任何输出,正确的做法是什么?
谢谢
如果你想使用 GUIDE
工具精确复制教程的 GUI,首先你必须注意到在教程中时钟是使用 text
uicontrol 创建的,所以它很清楚为什么要在 GUI 中插入 axes
.
还有。 GUI "window" (figure
) 是由 GUIDE 自动创建的,所以不需要添加轴,除非你想创建一个 "analog" 带有时针、分针的时钟等,但是,在这种情况下,GUI 将变得更加复杂。
使用GUIDE
你必须简单地添加一个text uicontrol
,然后,如果你想精确地重现教程中时钟的形状,你可以设置figure
和 text
uicontrol 通过 Property Inspector
.
创建 GUI 后,您可以在其 .m
文件中插入教程中的函数,以使其正常工作,如下所示:
- 在 GUI 中插入用于创建计时器及其初始化的代码
OpeningFcn
- 使用图中的
handles
在figure uicontrols
之间共享变量
- 添加图形的 handel 作为
updater
和deleter
函数的附加输入参数
然后您只需复制并粘贴 updater
和 deleter
函数,varargin
cellarray 的第三个元素将是您在其中设置的图形句柄结构,在OpeningFcn
timer
数据。
关于教程中的代码,您必须在 updater
函数中添加以下行:
fig_handles=varargin{3}
检索 text
uicontrol 的句柄。
使用 GUIDE 创建的整个 GUI 如下所示(代码中的注释应突出显示上述相关修改):
function varargout = GUI_1_GUIDE(varargin)
% GUI_1_GUIDE MATLAB code for GUI_1_GUIDE.fig
% GUI_1_GUIDE, by itself, creates a new GUI_1_GUIDE or raises the existing
% singleton*.
%
% H = GUI_1_GUIDE returns the handle to a new GUI_1_GUIDE or the handle to
% the existing singleton*.
%
% GUI_1_GUIDE('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GUI_1_GUIDE.M with the given input arguments.
%
% GUI_1_GUIDE('Property','Value',...) creates a new GUI_1_GUIDE or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before GUI_1_GUIDE_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to GUI_1_GUIDE_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help GUI_1_GUIDE
% Last Modified by GUIDE v2.5 16-Aug-2018 18:29:37
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @GUI_1_GUIDE_OpeningFcn, ...
'gui_OutputFcn', @GUI_1_GUIDE_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before GUI_1_GUIDE is made visible.
function GUI_1_GUIDE_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to GUI_1_GUIDE (see VARARGIN)
% Choose default command line output for GUI_1_GUIDE
handles.output = hObject;
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Insert here the code for the creation of the timer and its initialization
% Use the figure's handles to share the variables among the figure
% uicontrols
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
handles.text_clock.String=datestr(now,16);
handles.STRT = 60 - str2double(datestr(now,'ss')); % So we can update every minute.
handles.tmr = timer('Name','Reminder',...
'Period',60,... % Update the time every 60 seconds.
'StartDelay',handles.STRT,... % In seconds.
'TasksToExecute',inf,... % number of times to update
'ExecutionMode','fixedSpacing')
set(handles.tmr,'TimerFcn',{@updater handles});
%
% Add the figure's handels as additional input parameter of the "updater"
% and "deleter" function
%
set(hObject,'deletefcn',{@deleter handles}) % Kill timer if fig is closed.
start(handles.tmr); % Start the timer object.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes GUI_1_GUIDE wait for user response (see UIRESUME)
% uiwait(handles.figure1);
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Add the "updater" and "deleter" function
% The third element of the "varargin" parameter is the figure's handle
% struct
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
function [] = updater(varargin)
% timerfcn for the timer. If figure is deleted, so is timer.
% I use a try-catch here because timers are finicky in my
% experience.
%
fig_handles=varargin{3}
try
set(fig_handles.text_clock,'string',datestr(now,16))
if ~str2double(datestr(now,'MM'))
X = load('gong'); % At the hour, sound a gong.
sound(X.y,X.Fs*2.5)
end
clear X
catch
delete(fig_handles.figure1) % Close it all down.
end
function [] = deleter(varargin)
% If figure is deleted, so is timer.
fig_handles=varargin{3}
stop(fig_handles.tmr);
delete(fig_handles.tmr);
% --- Outputs from this function are returned to the command line.
function varargout = GUI_1_GUIDE_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;