Created
November 7, 2010 14:13
-
-
Save andreif/666142 to your computer and use it in GitHub Desktop.
A better alternative to MATLAB's disp()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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