Search code examples
matlabvariadic-functionscell-array

Passing variable number of strings to MATLAB function from varagin


I have a function where I am receiving parameters as part of a varargin parameter. However, within that function is another function call using varargin. How can I pass the contents of the first varargin to the nested function without ended up with a cell array of cell arrays?

That is, given the following code:

function foo(varargin)
    bar(varargin)
end

function bar(varargin)
    % Do something with varargin
end

foo('ab', 'cdef')

the varargin in bar(varargin) is a 1x1 cell array containing the 1x2 cell array {'ab', 'cdef'}.

I'm trying instead to write something where:

foo('ab', 'cdef')

can give a bar(varargin) call where the varargin is just a 1x2 cell array {'ab', 'cdef'}.

How can I transfer the contents of the varargin cell array in the foo call to just be individual parameters in the bar call?


Solution

  • Simply bring the elements out of the cell array by using the {:} colon argument

    function foo(varargin)
        bar(varargin{:});
    end