######################################################################## ## ## Copyright (C) 1996-2020 The Octave Project Developers ## ## See the file COPYRIGHT.md in the top-level directory of this ## distribution or . ## ## This file is part of Octave. ## ## Octave is free software: you can redistribute it and/or modify it ## under the terms of the GNU General Public License as published by ## the Free Software Foundation, either version 3 of the License, or ## (at your option) any later version. ## ## Octave is distributed in the hope that it will be useful, but ## WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with Octave; see the file COPYING. If not, see ## . ## ######################################################################## ## -*- texinfo -*- ## @deftypefn {Function File} {@var{vec} =} sprintfc (@var{template}, ...) ## Print optional numerical arguments under the control of the template string ## @var{template} in a cell array. ## ## @seealso{printf, sprintf} ## @end deftypefn ## Author: Massimiliano Fasi function vec = sprintfc (varargin) if (nargin < 1) print_usage (); endif if (nargin == 1) vec = {sprintf(varargin{1})}; else template = varargin{1}; if (ischar (varargin{2})) error ('Octave:invalid-input-arg', "sprintfc: input string vectors not allowed"); else data = varargin{2}; arraylength = length (data); endif for i = 3:nargin if (length (varargin{i}) != arraylength) error ('Octave:invalid-input-arg', "sprintfc: all the vectors must have the same length"); endif if (ischar (varargin{i})) error ('Octave:invalid-input-arg', "sprintfc: input string vectors not allowed"); endif data = [data; varargin{i}]; endfor tmp = strread (sprintf([template,'$'], data), '%s', 'delimiter', '$'); vec = reshape( tmp, [1, arraylength]); endif endfunction %!assert(sprintfc(''), {char(ones(1,0))}); %!assert(sprintfc('Hello World!'), {'Hello World!'}); %!assert(sprintfc('%d',1), {'1'}); %!assert(sprintfc('%d', 1:10), {'1','2','3','4','5','6','7','8','9','10'}); %!assert(sprintfc('%d%d', 1:5, 5:-1:1),{'15','24','33','42','51'});