function theResult = demonstrate(varargin) % demonstrate -- Evaluate and display a command. % demonstrate ... or demonstrate('...') evaluates % the concatenated argument(s) as a single command % and returns or displays a single string showing % the answer adjacent to the command itself. % demonstrate (no argument) shows help and % demonstrates several commands. % Copyright (C) 1998 Dr. Charles R. Denham, ZYDECO. % All Rights Reserved. % Disclosure without explicit written consent from the % copyright owner does not constitute publication. % Version of 14-Aug-1998 09:12:02. if nargin < 1 & nargout < 1 help(mfilename) demonstrate datestr(now) demonstrate sqrt(2) demonstrate min(2, 3) demonstrate 2 * 3 oldWarning = warning; warning off demonstrate [-1 0 1] / 0 warning(oldWarning) return end theCommand = ''; for i = 1:length(varargin) if i > 1, theCommand = [theCommand ' ']; end theCommand = [theCommand varargin{i}]; end okay = 1; theAnswer = []; lasterr('') theAnswer = evalin('caller', theCommand, '[]'); if ~isempty(lasterr), theAnswer = ['Error: ' lasterr]; end result = [theCommand ' ==> ' mat2str(theAnswer)]; if nargout > 0 theResult = result; else disp([' ' result]) end