jakob
jakob

Reputation:

MATLAB: calling GUI/GUIDE functions from outside

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:

###############################################################
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?

Upvotes: 0

Views: 6035

Answers (2)

Sam Maloney
Sam Maloney

Reputation: 279

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).

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'.

Upvotes: 0

gnovice
gnovice

Reputation: 125874

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):

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

Upvotes: 2

Related Questions