figure
Creates a new figure for plotting and uicontrols.
Syntax
h = figure()
h = figure(n)
Inputs
- n
- Type: integer
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
The windowbuttondownfcn and windowbuttonupfcn funtions are triggered on mouse button press and mouse button release events respectively. The first argument of the function is the handle of the figure. The second argument is a scalar:
- 1 if the left mouse button was pressed/released.
- 2 if the middle mouse button was pressed/released.
- 3 if the right mouse button was pressed/released.
function onWindowButtonDown(handle, data)
point = get(handle, 'currentpoint');
disp(['Figure: ', num2str(handle), ', Button down: ', num2str(data), ', Point: (', num2str(point(1)), ',', num2str(point(2)),')']);
end
function onWindowButtonUp(handle, data)
point = get(handle, 'currentpoint');
disp(['Figure: ', num2str(handle), ', Button up: ', num2str(data), ', Point: (', num2str(point(1)), ',', num2str(point(2)),')']);
end
figure;
set(gcf,'windowbuttondownfcn', 'onWindowButtonDown');
set(gcf,'windowbuttonupfcn', 'onWindowButtonUp');
Usage of windowbuttonmotionfcn property:
function onWindowButtonMotion(handle, data)
point = get(handle, 'currentpoint');
disp(['Mouse motion, Point: (', num2str(point(1)), ',', num2str(point(2)),')']);
end
figure;
set(gcf,'windowbuttonmotionfcn', 'onWindowButtonMotion');
Usage of windowscrollwheelfcn property:
function onWindowScrollWheel(handle, data)
disp(['Mouse scroll, Direction: ', num2str(data.VerticalScrollCount)]);
end
figure;
set(gcf,'windowscrollwheelfcn', 'onWindowScrollWheel');
The keypressfcn and windowkeypressfcn functions are triggered on a key press event:
function onKeyPress(handle, data)
disp(['Key press: ', data.Key]);
end
function onWindowKeyPress(handle, data)
disp(['Window key press: ', data.Key]);
end
figure;
h = uitree('text', 'My Tree', 'position', [10 10 200 300]);
node1 = uitreenode(h, 'text', 'Node');
leafnode = uitreenode(node1, 'text', 'Leaf Node');
% 'keypressfcn' is triggered only if the figure has focus
set(gcf,'keypressfcn',@onKeyPress)
% 'windowkeypressfcn' is triggered if the figure or a child of the figure, in this example the uitree, has focus
set(gcf,'windowkeypressfcn',@onWindowKeyPress)
The keyreleasefcn and windowkeyreleasefcn functions are triggered on a key release event:
function onKeyRelease(handle, data)
disp(['Key release: ', data.Key]);
end
function onWindowKeyRelease(handle, data)
disp(['Window key release: ', data.Key]);
end
figure;
h = uitree('text', 'My Tree', 'position', [10 10 200 300]);
node1 = uitreenode(h, 'text', 'Node');
leafnode = uitreenode(node1, 'text', 'Leaf Node');
% 'keyreleasefcn' is triggered only if the figure has focus
set(gcf,'keyreleasefcn',@onKeyRelease)
% 'windowkeyreleasefcn' is triggered if the figure or a child of the figure, in this example the uitree, has focus
set(gcf,'windowkeyreleasefcn',@onWindowKeyRelease)
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.