Skip to content

Instantly share code, notes, and snippets.

@andreif
Created November 7, 2010 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreif/666142 to your computer and use it in GitHub Desktop.
Save andreif/666142 to your computer and use it in GitHub Desktop.
A better alternative to MATLAB's disp()
function pr(varargin)
if nargin == 0, return, end % stop if no arguments
% check if the first argument is a string with output format:
if ischar(varargin{1}) && ~isempty(regexp(varargin{1},'\%[^\s]','ONCE'))
cmd = 'sprintf(varargin{1}';
for i=2:nargin,
if iscell(varargin{i}),
% if an argument is a cell array, then consider it as collected parameters:
for j=1:length(varargin{i})
cmd = sprintf('%s, varargin{%d}{%d}', cmd, i, j);
end
else
cmd = sprintf('%s, varargin{%d}', cmd, i);
end
end
if nargout,
% todo: add a return value later
else
disp(eval(strcat(cmd, ');')));
end
else % simply display all arguments one-by-one:
for i=1:nargin, disp(varargin{i}), end
end
% --- examples ----------------------------------------------------------
% >> pr(1,2,3)
% 1
%
% 2
%
% 3
%
% >> pr('%f,',1,2,3)
% 1.000000,2.000000,3.000000,
%
% >> pr('total core power [MWth] = ',P_core)
% total core power [MWth] =
% 600
%
% >> pr('total core power = %.1f MWth',P_core)
% total core power = 600.0 MWth
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment