简体   繁体   中英

Matlab - Pass varargin to a function accepting variable number of arguments

Want to write a shorthand for fprintf(..) .

varargin is a cell array. So how can I pass it to fprintf(..) ? The latter only accepts a variable number of arrays.

The following doesn't work:

function fp(str, varargin)
    fprintf(str, varargin);
end

Giving

Error using fprintf
Function is not defined for 'cell' inputs.

or

Error: Unexpected MATLAB expression.

The solution is:

function fp(str, varargin)
    fprintf(str, varargin{:});
end

The cell array is expanded into a comma-separated list using the {:} syntax.

A shortcut using an anonymous function is

fp = @(str, varargin) fprintf(str, varargin{:});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM