Code covered by the BSD License  

Highlights from
IPTImages

image thumbnail
from IPTImages by Brett Shoelson
Returns a list or selectable UI of images in specified directory. By default, shows IPT demo images.

saveTo(varname,value,workspace)
function saveTo(varname,value,workspace)
% SAVETO Saves a variable to specified workspace and (unique) name
%
% SAVETO(VARNAME,VALUE,WORKSPACE) saves the variable in the input
% VALUE, using the name specified by VARNAME, in the workspace
% specified by WORKSPACE. If the variable already exists in the
% specified workspace, a number is appended to it, and
% incremented as necessary, to ensure unique variable names.
%
% This is is a convenient way to save incremental
% values/varialbes without worrying about stepping on existing
% variables.
%
% EXAMPLE:
% (Assumes the variable 'X' does not initially exist in the base
% workspace).
%
% saveTo('X',magic(4),'base')
%    % Specified value written to X in base workspace.
% saveTo('X',magic(4),'base')
%    % Specified value written to X1 in base workspace.
% saveTo('X',magic(4),'base')
%    % Specified value written to X2 in base workspace.
% 
% whos X*
%    % Name      Size            Bytes  Class     Attributes
%    % X         4x4               128  double              
%    % X1        4x4               128  double              
%    % X2        4x4               128  double   
%
% Written by Brett Shoelson, PhD.
% brett.shoelson@mathworks.com
% 04/28/2009
 
if nargin < 3
    error('SaveTo: Requires at least two input arguments, specifying the VARNAME to write to, and the VALUE to be written.')
end
if nargin == 2
    workspace = 'base';
end

workspace = lower(workspace);

if ~ismember(workspace,{'base','caller'})
    error('SaveTo: Supports writing to BASE or CALLER workspaces.');
end

n = 0; tmp = 1;
while tmp
    if n == 0
        tmp = evalin(workspace,['exist(''' varname ''')']);
    else
        tmp = evalin(workspace,['exist(''' varname, num2str(n), ''')']);
    end
    if tmp
        n = n + 1;
    end
end

try
    if n == 0
        assignin(workspace,varname,value);
        fprintf('Specified value written to %s in %s workspace.\n',varname,workspace);
    else
        assignin(workspace,[varname num2str(n)],value);
        fprintf('Specified value written to %s in %s workspace.\n',[varname,num2str(n)],workspace);
    end
catch
    error('SaveTo: Unable to write value to specified workspace.')
end

Contact us at files@mathworks.com