Code covered by the BSD License  

Highlights from
Snapshot

image thumbnail
from Snapshot by Douglas Schwarz
Program to take screenshot of modal GUI figures.

snapshot
function snapshot
%snapshot: GUI program to take snapshot of modal GUI figures.
%
% Syntax:
%   snapshot
%
% Snapshot is used to take an image snapshot of a figure, especially one
% containing a modal GUI.  Normally, one would use getframe from the
% command line to take such a snapshot, but this doesn't work with a modal
% GUI because the command line is not available while the GUI is in
% operation.  Using Snapshot you can trigger a getframe operation by
% operating in the modal GUI.  You can choose what kind of operation is
% used to trigger the getframe, 'ButtonDownFcn', 'KeyPressFcn', or
% 'WindowButtonDownFcn' -- simply choose an operation that is not used by
% the GUI.  If none of these are available or you're not sure you can also
% trigger the getframe with a timer.  This works very much like the self
% timer on your camera -- just start the timer and you have a set time in
% which to get the GUI on-screen and configured the way you want.  The
% resulting image is exported into the base workspace with the specified
% variable name.
%
% Snapshot captures the whole figure window, including the title bar and
% the borders.
%
% For example, start Snapshot and choose ButtonDownFcn.  Next start your
% modal GUI function, make it look the way you want and then click on the
% figure where there is no other object present.  You will see a brief
% flash as a white background figure is created behind your figure and an
% image of your figure will be placed in a variable in the base workspace
% (default name is 'snap').  Then just close your GUI and Snapshot.

% Version: 1.0, 23 April 2006
% Author:  Douglas M. Schwarz
% Email:   dmschwarz=ieee*org, dmschwarz=urgrad*rochester*edu
% Real_email = regexprep(Email,{'=','*'},{'@','.'})


% List of root properties used.
fcns = {'DefaultFigureButtonDownFcn','DefaultFigureKeyPressFcn',...
	'DefaultFigureWindowButtonDownFcn'};
default_fcn_index = 1;

% Defaults in use before snapshot is used.
init_fcns = get(0,fcns);

% Create GUI figure and all the uicontrols.  Set some parameters.
bg_color = get(0,'DefaultUIControlBackgroundColor');
fig = figure('Position',[0 0 180 215],...
	'Color',bg_color,...
	'Resize','off',...
	'NumberTitle','off',...
	'Name','Snapshot',...
	'IntegerHandle','off',...
	'Menubar','none',...
	'CloseRequestFcn',@quit,...
	'CreateFcn',{@movegui,'northwest'});

fcn_index = default_fcn_index;
delay = 10;
delay_str = sprintf('%g',delay);
snap_var_name = 'snap';
snap_timer = [];
% Fudge factor so whole window is captured.
if ispc
	pos_fudge = [1 1 0 -1];
else
	% Works on Mac OS X, not sure about Linux or other unix.
	pos_fudge = [1 1 0 1];
end

% Radio buttons.
but = zeros(1,4);
common.Style = 'radiobutton';
common.Callback = @radio;
but(1) = uicontrol(common,'Position',[20 175 100 20],...
	'String','ButtonDownFcn');
but(2) = uicontrol(common,'Position',[20 150 100 20],...
	'String','KeyPressFcn');
but(3) = uicontrol(common,'Position',[20 125 140 20],...
	'String','WindowButtonDownFcn');
but(4) = uicontrol(common,'Position',[20 100 60 20],...
	'String','Timer,');
timer_but_index = 4;
% Delay edit box.
delay_ed = uicontrol('Style','edit',...
	'Position',[80 100 30 20],...
	'String',delay_str,...
	'BackgroundColor','w',...
	'HorizontalAlignment','center',...
	'Callback',@change_delay);
uicontrol('Style','text',...
	'Position',[110 100 25 16],...
	'HorizontalAlignment','left',...
	'String','sec.');
% Start button.
start_but = uicontrol('Style','pushbutton',...
	'Position',[40 70 60 20],...
	'String','Start',...
	'Callback',@start_timer,...
	'Enable','off');

uicontrol('Style','frame',...
	'Position',[15 60 150 1],...
	'ForegroundColor',bg_color*0.75)
uicontrol('Style','text',...
	'Position',[20 40 140 16],...
	'String','Export as:',...
	'HorizontalAlignment','left')
% Variable name edit box.
var_name_ed = uicontrol('Style','edit',...
	'Position',[20 20 140 20],...
	'String',snap_var_name,...
	'BackgroundColor','w',...
	'HorizontalAlignment','left',...
	'Callback',@change_var_name);

set(fig,'HandleVisibility','off')

set(but(fcn_index),'Value',1)
set(0,fcns{fcn_index},@snap)
if fcn_index == timer_but_index
	set(start_but,'Enable','on')
end

% -------------------- Callback functions --------------------

	% Callback function to take snapshot.
	function snap(varargin)
		% Get subject figure and positions.
		subject_fig = varargin{1};
		pos = get(subject_fig,'Position');
		opos = get(subject_fig,'OuterPosition');
		rect = [opos(1:2)-pos(1:2),opos(3:4)] + pos_fudge;
		% Create a white background behind subject figure.
		backdrop = figure('Position',opos + [-10 -10 20 20],...
			'Color','w',...
			'DeleteFcn','');
		figure(subject_fig)
		% Get snapshot and delete backdrop.
		frame = getframe(subject_fig,rect);
		disp('Snap!')
		delete(backdrop)
		% Export snapshot to base workspace.
		assignin('base',snap_var_name,frame.cdata)
	end

	% Callback function to take snapshot with timer.
	function timer_snap(varargin)
		% Delete timer object.
		stop(snap_timer)
		delete(snap_timer)
		snap_timer = [];
		% Get subject figure and positions.
		all_figs = findall(0,'Type','figure');
		if isempty(all_figs)
			return
		else
			subject_fig = all_figs(1);
		end
		pos = get(subject_fig,'Position');
		opos = get(subject_fig,'OuterPosition');
		rect = [opos(1:2)-pos(1:2),opos(3:4)] + pos_fudge;
		% Create a white background behind subject figure.
		backdrop = figure('Position',opos + [-10 -10 20 20],...
			'Color','w',...
			'DeleteFcn','');
		figure(subject_fig)
		% Get snapshot and delete backdrop.
		frame = getframe(subject_fig,rect);
		disp('Snap! (timer)')
		delete(backdrop)
		% Export snapshot to base workspace.
		assignin('base',snap_var_name,frame.cdata)
		% Re-enable Start button.
		set(start_but,'Enable','on')
	end

	% Callback function to handle radio buttons.
	function radio(varargin)
		% Use old value of fcn_index.
		set(but(fcn_index),'Value',0)
		if fcn_index == timer_but_index
			set(start_but,'Enable','off')
		else
			set(0,fcns{fcn_index},init_fcns{fcn_index})
		end
		% Get new value of fcn_index.
		fcn_index = find(varargin{1} == but);
		% Use new value of fcn_index.
		set(but(fcn_index),'Value',1)
		if fcn_index == timer_but_index
			set(start_but,'Enable','on')
		else
			set(0,fcns{fcn_index},@snap)
		end
	end

	% Callback function to handle setting timer delay.
	function change_delay(varargin)
		delay_str = get(delay_ed,'String');
		delay = sscanf(delay_str,'%f');
	end

	% Callback function to handle specifying variable name.
	function change_var_name(varargin)
		snap_var_name = get(var_name_ed,'String');
	end

	% Callback function to start timer.
	function start_timer(varargin)
		snap_timer = timer('TimerFcn',@timer_snap,'Period',delay,...
			'StartDelay',delay);
		start(snap_timer)
		set(start_but,'Enable','off')
	end

	% Callback function to close the GUI.
	function quit(varargin)
		if ~isempty(snap_timer)
			stop(snap_timer)
			delete(snap_timer)
			snap_timer = [];
		end
		set(0,fcns,init_fcns)
		delete(fig)
	end

end

Contact us at files@mathworks.com