MATLAB variable arguments of variable length read from variable

I have a function with variable arguments declared in a standard way:

[] = foo ( varargin )

and I would like to call it from another function, but specify the arguments programmatically. My best attempt is as follows:

% bar isn't populated like this, but this is how it ends up
bar = { 'var1' 'var2' 'var3' }; 
foo( bar );

However, the bar is placed in an array of 1x1 cells and is not interpreted as an array of 1x3 cells, as I expected. I can't change foo, so is there a workaround?

+3
source share
2 answers

If you have variables a, band cthat you want to put together somewhere and end up moving to a function as a series of inputs, you can do the following:

inArgs = {a b c};  % Put values in a cell array
foo(inArgs{:});

inArgs{:} , . , :

foo(a,b,c);

foo , , varargin 1- by-3, . , varargin inArgs. foo {:}:

foo(inArgs);

varargin 1 1, inArgs. , foo 1 ( 1 3).

+10

, , - eval, MATLAB, .

"'var1', 'var2', 'var3'", :

eval(["foo(", barString, ")"])

, , , .

0

Source: https://habr.com/ru/post/1712679/


All Articles