Code covered by the BSD License  

Highlights from
figtagged

figtagged

by

 

Return the handle to a figure with a specified tag

figtagged(thetag)
function varargout = figtagged(thetag)
%figtagged Return the handle to a figure with a specified tag.
%   hfig = figtagged(thetag) returns a handle to a figure window with the
%   the 'Tag' property set to the string held in thetag. If there is
%   already a figure window with the specified tag, it will be cleared and
%   reused; otherwise, a new figure is made.
%
%   See also: figure.

error(nargchk(0, 1, nargin, 'struct')); %#ok<NCHKN>
assert(ischar(thetag), 'figtagged:nonCharInput', ...
    'Inputs must be of type ''char''.');
error(nargchk(0, 1, nargout, 'struct')); %#ok<NCHKM>

allMatches = findall(0, 'Type', 'figure', 'Tag', thetag);

% If the figure doesn't already exist, make it.
noFigureExists = isempty(allMatches);
if noFigureExists  
    hfig = figure();   
    set(hfig, 'Tag', thetag);    
else
    figureIsUnique = isscalar(allMatches);
    if ~figureIsUnique
        close(allMatches(2:end));
        warning('figtagged:tagNotUnique', ...
            ['Found more than one figure with the specified ''Tag'' ' ...
            'property. Closing duplicates.']);
    end
    hfig = allMatches(1);
    figure(hfig);
    clf(hfig);
end

% Only assign an argument if specifically requested - like FIGURE.
if nargout > 0
    varargout = {hfig};
end

Contact us