Home > code > misc > disp_once.m

disp_once

PURPOSE ^

Display a specific message only once (per MATLAB session).

SYNOPSIS ^

function got_displayed = disp_once(varargin)

DESCRIPTION ^

 Display a specific message only once (per MATLAB session).
 GotDisplayed = disp_once(Message,Arguments...)

 This function displays a message like fprintf (though with implicit newline), except that a 
 message with the given content is only emitted once per MATLAB session.

 In:
   Message : the message content (as in fprintf)
             special case: if this is 'clear', the memory of displayed messages will be discarded

   Arguments... : list of arguments that may be substituted into Message (as in fprintf)

 Out:
   GotDisplayed : whether the message got displayed

 Examples:
   % display a message, but just once
   disp_once('Note: Trying to read file now...');

 See also:
   disp

                                Christian Kothe, Swartz Center for Computational Neuroscience, UCSD
                                2011-02-13

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function got_displayed = disp_once(varargin)
0002 % Display a specific message only once (per MATLAB session).
0003 % GotDisplayed = disp_once(Message,Arguments...)
0004 %
0005 % This function displays a message like fprintf (though with implicit newline), except that a
0006 % message with the given content is only emitted once per MATLAB session.
0007 %
0008 % In:
0009 %   Message : the message content (as in fprintf)
0010 %             special case: if this is 'clear', the memory of displayed messages will be discarded
0011 %
0012 %   Arguments... : list of arguments that may be substituted into Message (as in fprintf)
0013 %
0014 % Out:
0015 %   GotDisplayed : whether the message got displayed
0016 %
0017 % Examples:
0018 %   % display a message, but just once
0019 %   disp_once('Note: Trying to read file now...');
0020 %
0021 % See also:
0022 %   disp
0023 %
0024 %                                Christian Kothe, Swartz Center for Computational Neuroscience, UCSD
0025 %                                2011-02-13
0026 
0027 % Copyright (C) Christian Kothe, SCCN, 2011, christian@sccn.ucsd.edu
0028 %
0029 % This program is free software; you can redistribute it and/or modify it under the terms of the GNU
0030 % General Public License as published by the Free Software Foundation; either version 2 of the
0031 % License, or (at your option) any later version.
0032 %
0033 % This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
0034 % even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0035 % General Public License for more details.
0036 %
0037 % You should have received a copy of the GNU General Public License along with this program; if not,
0038 % write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
0039 % USA
0040 
0041 persistent displayed_messages;
0042 
0043 if strcmp(varargin{1},'clear')
0044     % clear the stored messages
0045     displayed_messages = [];
0046     return;
0047 end
0048 
0049 message_content = sprintf(varargin{1:end});
0050 
0051 % generate a hash of it
0052 str = java.lang.String(message_content);
0053 message_id = sprintf('x%.0f',str.hashCode()+2^31);
0054 
0055 % and check if it had been displayed before
0056 if ~isfield(displayed_messages,message_id)
0057     % emit the message
0058     disp(message_content);
0059     got_displayed = true;
0060     % remember to not display the message again
0061     displayed_messages.(message_id) = true;
0062 else
0063     got_displayed = false;
0064 end
0065

Generated on Tue 20-Aug-2013 03:44:10 by m2html © 2005