figure

Creates a new figure for plotting and uicontrols.

Syntax

h = figure()

h = figure(n)

Inputs

n
Type: integer
Dimension: scalar

Outputs

h
Handle of the figure.

Examples

Simple figure example:
figure (2)
Usage of createfcn and deletefcn properties:
function onCreate(handle, data)
  data; % data is not used
  disp(['Figure ', num2str(handle), ' is created'])
end

function onDelete(handle, data)
  data; % data is not used
  disp(['Figure ', num2str(handle), ' is deleted'])
end

figure('createfcn', 'onCreate', 'deletefcn','onDelete');

disp('Pausing for 1 sec...')
pause(1)

disp('Deleting figure')
close(gcf)
      
Usage of sizechangedfcn property:
function onSizeChanged(handle, data)
  data; % data is not used
  disp(['Figure ', num2str(handle), ' is resized'])
end

figure('sizechangedfcn','onSizeChanged');

% use the mouse to resize the figure and check the function output in the console
      
Usage of keypressfcn property:
function onKeyPress(handle, data)
  data
  disp(['Figure ', num2str(handle), ' Key pressed: ', data.Character])
end

figure('keypressfcn','onKeyPress');

% click on the figure to give focus, press any key and check the function output in the console
      

Comments

If called without an argument, a new figure is created with the next available ID. If called with a free ID, a new figure is created with the free ID assigned to it. If called with a used ID, no figure is created but the figure with the ID is set as the current figure.