MATLAB:从外部调用GUI / GUIDE函数

时间:2023-01-15 16:56:43

When I try to invoke a subfunction in a GUI/GUIDE file (using a function handle which has been exposed as a global variable), a new axes is always created even if I set the axes to a specific axes in the GUIDE figure. Does anyone know why this is happening? GUIDE code is:

当我尝试在GUI / GUIDE文件中调用子函数(使用已作为全局变量公开的函数句柄)时,即使我将轴设置为GUIDE图中的特定轴,也始终会创建新轴。有谁知道为什么会这样? GUIDE代码是:

###############################################################
function varargout = demo(varargin)
  % Begin initialization code - DO NOT EDIT
  gui_Singleton = 1;
  gui_State = struct('gui_Name',       mfilename, ...
                     'gui_Singleton',  gui_Singleton, ...
                     'gui_OpeningFcn', @demo_OpeningFcn, ...
                     'gui_OutputFcn',  @demo_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

function demo_OpeningFcn(hObject, eventdata, handles, varargin)
  handles.output = hObject;

  % Update handles structure
  guidata(hObject, handles);

  global myhandles updateFunction;
  myhandles = handles;
  updateFunction = @update;

function varargout = demo_OutputFcn(hObject, eventdata, handles) 
  varargout{1} = handles.output;

function pushbutton1_Callback(hObject, eventdata, handles)
  update();

function update()
  global myhandles;

  axes(myhandles.axes1);
  plot(1:2,1:2);

###########################################################################

And when I do (outside file above):

当我这样做时(上面的文件):

global updateFunction;
feval(updateFunction)

I always see the plot in a newly created figure window, not in the GUI figure. Why is this happening?

我总是在新创建的图形窗口中看到该图,而不是在GUI图中。为什么会这样?

2 个解决方案

#1


The first thing I would try is to replace the function update with the following:

我要尝试的第一件事是用以下代码替换函数更新:

function update
  global myhandles;
  plot(myhandles.axes1,1:2,1:2);

This will explicitly tell the PLOT function to plot into the given axes. If that doesn't work, try setting the axes 'NextPlot' property to 'add' (probably in demo_OpeningFcn):

这将明确告诉PLOT函数绘制到给定的轴。如果这不起作用,请尝试将轴的'NextPlot'属性设置为'add'(可能在demo_OpeningFcn中):

set(myhandles.axes1,'NextPlot','add');

#2


By default, when you create a GUI using GUIDE, Matlab sets the 'HandleVisibility' property of all objects associated with the GUI to 'callback'. This means that you cannot set these handles to be the current figure or current axis from anywhere outside of the callback routines (ie. from the command line or an external function).

默认情况下,使用GUIDE创建GUI时,Matlab会将与GUI关联的所有对象的“HandleVisibility”属性设置为“callback”。这意味着您无法将这些句柄设置为回调例程之外的任何位置(即来自命令行或外部函数)的当前图形或当前轴。

To work around this you can either specify the appropriate handle explicitly in all of your plotting functions or you can set the 'HandleVisibility' property of the axes to 'on'. This can be done on an object by object basis via the property inspector, or for the entire GUI by going to 'Tools -> GUI Options...' and changing the 'Command-line Accessibility:' to 'on'.

要解决此问题,您可以在所有绘图函数中明确指定相应的句柄,也可以将轴的“HandleVisibility”属性设置为“on”。这可以通过属性检查器逐个对象地完成,或者通过转到“工具 - > GUI选项...”并将“命令行可访问性:”更改为“打开”来完成整个GUI。

#1


The first thing I would try is to replace the function update with the following:

我要尝试的第一件事是用以下代码替换函数更新:

function update
  global myhandles;
  plot(myhandles.axes1,1:2,1:2);

This will explicitly tell the PLOT function to plot into the given axes. If that doesn't work, try setting the axes 'NextPlot' property to 'add' (probably in demo_OpeningFcn):

这将明确告诉PLOT函数绘制到给定的轴。如果这不起作用,请尝试将轴的'NextPlot'属性设置为'add'(可能在demo_OpeningFcn中):

set(myhandles.axes1,'NextPlot','add');

#2


By default, when you create a GUI using GUIDE, Matlab sets the 'HandleVisibility' property of all objects associated with the GUI to 'callback'. This means that you cannot set these handles to be the current figure or current axis from anywhere outside of the callback routines (ie. from the command line or an external function).

默认情况下,使用GUIDE创建GUI时,Matlab会将与GUI关联的所有对象的“HandleVisibility”属性设置为“callback”。这意味着您无法将这些句柄设置为回调例程之外的任何位置(即来自命令行或外部函数)的当前图形或当前轴。

To work around this you can either specify the appropriate handle explicitly in all of your plotting functions or you can set the 'HandleVisibility' property of the axes to 'on'. This can be done on an object by object basis via the property inspector, or for the entire GUI by going to 'Tools -> GUI Options...' and changing the 'Command-line Accessibility:' to 'on'.

要解决此问题,您可以在所有绘图函数中明确指定相应的句柄,也可以将轴的“HandleVisibility”属性设置为“on”。这可以通过属性检查器逐个对象地完成,或者通过转到“工具 - > GUI选项...”并将“命令行可访问性:”更改为“打开”来完成整个GUI。