Matlab has a well defined method for passing variable numbers of arguments to functions, using expansion of cell arrays into . You would thus have to transform your input matrix into a cell array, then expand that cell array into a c-s-l. Now, the best place to do that conversion would be inside |fLogLik| keeping the |par| as an array until then, so your anonymous function becomes fLL = @(par) - fLogLik(par, mData); and |fLogLik|: function x = fLogLik(par, mData) %... par = num2cell(par); %convert to cell array mQ = diag(par{:}); %expand cell array to comma-separated list. %... end If you really insist that |fLogLik| must have a variable numbers of arguments, then a) you need to change the order so that the fixed argument |mData| is first, then use |varargin| for the variable arguments: function x = fLogLik(mData, varargin) %... mQ = diag(varargin{:}); %... end b) You have to do the conversion from matrix to cell array in |fLL|.Unfortunately, matlab chaining rules prevents you from doing that into an anonymous function. Thus, you'll have to use a standard function. A nested function would be best if you want to use your |iS| and |mdata| variables, although |iS| is not really needed in the body of the function except maybe for validation: iS = 4; function x = fLL(par) assert(numel(par) == iS, 'Wrong size of array passed to function'); %if validation desired par = num2cell(par); %convert to cell array x = fLogLik(mdata, par{:}); %expand cell array into comma separated list %matlab chaining rules prevent using : num2cell(par){:} end Answer from Guillaume on mathworks.com
🌐
MathWorks
mathworks.com › matlab › programming › functions › argument definitions
Support Variable Number of Inputs - MATLAB & Simulink
This example shows how to define a function that accepts a variable number of input arguments using varargin.
🌐
MathWorks
mathworks.com › matlab › programming › functions › argument definitions
varargin - Variable-length input argument list - MATLAB
You can use the varargin in a MATLAB Function block that has the HDL block property Architecture set to MATLAB Datapath and multi-index input arguments into cell arrays. For example, you can now generate HDL code for this code snippet: ... Specifying a variable number of input arguments using varargin shows improved performance.
🌐
MathWorks
mathworks.com › matlabcentral › answers › 278375-function-with-variable-amount-of-inputs
function with variable amount of inputs - MATLAB Answers - MATLAB Central
April 11, 2016 - Or for the general case you can use varargin: this has the disadvantage that the inputs are not named, so tab completion and the code helper tools will only show varargin instead of more useful variable names. Sign in to comment. ... https://www.mathworks.com/matlabcentral/answers/278375-function-with-variable-amount-of-inputs#answer_217386
Top answer
1 of 1
1
Matlab has a well defined method for passing variable numbers of arguments to functions, using expansion of cell arrays into . You would thus have to transform your input matrix into a cell array, then expand that cell array into a c-s-l. Now, the best place to do that conversion would be inside |fLogLik| keeping the |par| as an array until then, so your anonymous function becomes fLL = @(par) - fLogLik(par, mData); and |fLogLik|: function x = fLogLik(par, mData) %... par = num2cell(par); %convert to cell array mQ = diag(par{:}); %expand cell array to comma-separated list. %... end If you really insist that |fLogLik| must have a variable numbers of arguments, then a) you need to change the order so that the fixed argument |mData| is first, then use |varargin| for the variable arguments: function x = fLogLik(mData, varargin) %... mQ = diag(varargin{:}); %... end b) You have to do the conversion from matrix to cell array in |fLL|.Unfortunately, matlab chaining rules prevents you from doing that into an anonymous function. Thus, you'll have to use a standard function. A nested function would be best if you want to use your |iS| and |mdata| variables, although |iS| is not really needed in the body of the function except maybe for validation: iS = 4; function x = fLL(par) assert(numel(par) == iS, 'Wrong size of array passed to function'); %if validation desired par = num2cell(par); %convert to cell array x = fLogLik(mdata, par{:}); %expand cell array into comma separated list %matlab chaining rules prevent using : num2cell(par){:} end
Top answer
1 of 3
2

Use fprintf with varargin for this:

f = @(varargin) fprintf('var%i= %i\n', [(1:numel(varargin));[varargin{:}]])
f(5,6,7,88)
var1= 5
var2= 6
var3= 7
var4= 88

The format I've used is: 'var%i= %i\n'. This means it will first write var then %i says it should input an integer. Thereafter it should write = followed by a new number: %i and a newline \n.

It will choose the integer in odd positions for var%i and integers in the even positions for the actual number. Since the linear index in MATLAB goes column for column we place the vector [1 2 3 4 5 ...] on top, and the content of the variable in the second row.

By the way: If you actually want it on the format you specified in the question, skip the \n:

f = @(varargin) fprintf('var%i= %i', [(1:numel(varargin));[varargin{:}]])

f(6,12,3,15,5553)
var1= 6var2= 12var3= 3var4= 15var5= 5553

Also, you can change the second %i to floats (%f), doubles (%d) etc.

If you want to use actual variable names var1, var2, var3, ... in your input then I can only say one thing: Don't! It's a horrible idea. Use cells, structs, or anything else than numbered variable names.

Just to be crytsal clear: Don't use the output from this in MATLAB in combination with eval! eval is evil. The Mathworks actually warns you about this in the official documentation!

2 of 3
0

How about calling the function as many times as the number of parameters? I wrote this considering the specific form of the character string returned by your function where k is assumed to be the index of the 'kth' variable to be entered. Array var can be the list of your numeric parameters.

file=@(var,i)[strcat('var',num2str(i),'=') num2str(var) ];

var=[2,3,4,5];

str='';

for i=1:length(var);

str=strcat(str,file(var(i),i));

end
🌐
GeeksforGeeks
geeksforgeeks.org › software engineering › function-with-variable-number-of-input-arguments-in-matlab
Function With Variable Number of Input Arguments in MATLAB - GeeksforGeeks
April 28, 2025 - A MATLAB function can take a variable number of input arguments, without the use of arrays or any additional functionality. The way to go about this in MATLAB is by using the varargin - which can be interpreted as VARiable ARGument INput.
Find elsewhere
🌐
MathWorks
mathworks.com › matlab › programming › functions › argument definitions
nargin - Number of function input arguments - MATLAB
This MATLAB function returns the number of function input arguments given in the call to the currently executing function.
Top answer
1 of 3
2

Couple of helpful points to construct a solution:

  • This post shows you how to construct a Cartesian product between arbitrary arrays using ndgrid.
  • cellfun accepts multiple cell arrays simultaneously, which you can use to index specific elements.
  • You can capture a variable number of arguments from a function using cell arrays, as shown here.

So let's get the inputs to ndgrid from your outermost array:

grids = cellfun(@(x) 1:numel(x), pth, 'UniformOutput', false);

Now you can create an index that contains the product of the grids:

index = cell(1, numel(pth));
[index{:}] = ndgrid(grids{:});

You want to make all the grids into column vectors and concatenate them sideways. The rows of that matrix will represent the Cartesian indices to select the elements of pth at each iteration:

index = cellfun(@(x) x(:), index, 'UniformOutput', false);
index = cat(2, index{:});

If you turn a row of index into a cell array, you can run it in lockstep over pth to select the correct elements and call mintersect on the result.

for i = index'
    indices = num2cell(i');
    selection = cellfun(@(p, i) p{i}, pth, indices, 'UniformOutput', false);
    mintersect(selection{:});
end

This is written under the assumption that pth is a row array. If that is not the case, you can change the first line of the loop to indices = reshape(num2cell(i), size(pth)); for the general case, and simply indices = num2cell(i); for the column case. The key is that the cell from of indices must be the same shape as pth to iterate over it in lockstep. It is already generated to have the same number of elements.

2 of 3
0

I believe this does the trick. Calls mintersect on all possible combinations of vectors in pth{k}{kk} for k=1:n and kk=1:length(pth{k}).

Using eval and messing around with sprintf/compose a bit. Note that typically the use of eval is very much discouraged. Can add more comments if this is what you need.

% generate some data
n = 5;
pth = cell(1,n);

for k = 1:n
    pth{k} = cell(1,randi([1 10]));
    for kk = 1:numel(pth{k})
        pth{k}{kk} = randi([1 100], randi([1 10]), 1);
    end
end

% get all combs
str_to_eval = compose('1:length(pth{%i})', 1:numel(pth));
str_to_eval = strjoin(str_to_eval,',');
str_to_eval = sprintf('allcomb(%s)',str_to_eval);
% use eval to get all combinations for a given pth
all_combs = eval(str_to_eval);

% and make strings to eval in intersect
comp = num2cell(1:numel(pth));
comp = [comp ;repmat({'%i'}, 1, numel(pth))];
str_pattern = sprintf('pth{%i}{%s},', comp{:});
str_pattern = str_pattern(1:end-1); % get rid of last ,

strings_to_eval = cell(length(all_combs),1);
for k = 1:size(all_combs,1)
    strings_to_eval{k} = sprintf(str_pattern, all_combs(k,:));
end

% and run eval on all those strings 
result = cell(length(all_combs),1);
for k = 1:size(all_combs,1)
    result{k} = eval(['mintersect(' strings_to_eval{k} ')']);
    %fprintf(['mintersect(' strings_to_eval{k} ')\n']); % for debugging
end

For a randomly generated pth, the code produces the following strings to evaluate (where some pth{k} have only one cell for illustration):

mintersect(pth{1}{1},pth{2}{1},pth{3}{1},pth{4}{1},pth{5}{1})
mintersect(pth{1}{1},pth{2}{1},pth{3}{1},pth{4}{2},pth{5}{1})
mintersect(pth{1}{1},pth{2}{1},pth{3}{1},pth{4}{3},pth{5}{1})
mintersect(pth{1}{1},pth{2}{1},pth{3}{2},pth{4}{1},pth{5}{1})
mintersect(pth{1}{1},pth{2}{1},pth{3}{2},pth{4}{2},pth{5}{1})
mintersect(pth{1}{1},pth{2}{1},pth{3}{2},pth{4}{3},pth{5}{1})
mintersect(pth{1}{2},pth{2}{1},pth{3}{1},pth{4}{1},pth{5}{1})
mintersect(pth{1}{2},pth{2}{1},pth{3}{1},pth{4}{2},pth{5}{1})
mintersect(pth{1}{2},pth{2}{1},pth{3}{1},pth{4}{3},pth{5}{1})
mintersect(pth{1}{2},pth{2}{1},pth{3}{2},pth{4}{1},pth{5}{1})
mintersect(pth{1}{2},pth{2}{1},pth{3}{2},pth{4}{2},pth{5}{1})
mintersect(pth{1}{2},pth{2}{1},pth{3}{2},pth{4}{3},pth{5}{1})
mintersect(pth{1}{3},pth{2}{1},pth{3}{1},pth{4}{1},pth{5}{1})
mintersect(pth{1}{3},pth{2}{1},pth{3}{1},pth{4}{2},pth{5}{1})
mintersect(pth{1}{3},pth{2}{1},pth{3}{1},pth{4}{3},pth{5}{1})
mintersect(pth{1}{3},pth{2}{1},pth{3}{2},pth{4}{1},pth{5}{1})
mintersect(pth{1}{3},pth{2}{1},pth{3}{2},pth{4}{2},pth{5}{1})
mintersect(pth{1}{3},pth{2}{1},pth{3}{2},pth{4}{3},pth{5}{1})
mintersect(pth{1}{4},pth{2}{1},pth{3}{1},pth{4}{1},pth{5}{1})
mintersect(pth{1}{4},pth{2}{1},pth{3}{1},pth{4}{2},pth{5}{1})
mintersect(pth{1}{4},pth{2}{1},pth{3}{1},pth{4}{3},pth{5}{1})
mintersect(pth{1}{4},pth{2}{1},pth{3}{2},pth{4}{1},pth{5}{1})
mintersect(pth{1}{4},pth{2}{1},pth{3}{2},pth{4}{2},pth{5}{1})
mintersect(pth{1}{4},pth{2}{1},pth{3}{2},pth{4}{3},pth{5}{1})
🌐
MathWorks
mathworks.com › matlabcentral › answers › 97278-can-my-s-function-have-variable-number-of-inputs-and-outputs
Can my S-Function have variable number of inputs and outputs? - MATLAB Answers - MATLAB Central
June 27, 2009 - Please see the attached C-File "testtimestwo.c" which demonstrates the S-Function with variable number of inputs. For example, you can execute the attached example by performing the following steps: ... You will notice that the number of inputs ...
🌐
MathWorks
mathworks.com › matlab › programming › functions
Argument Definitions - MATLAB & Simulink
... Define required and optional inputs, assign defaults to optional inputs, and validate all inputs to a custom function using the Input Parser. ... Support Variable Number of Inputs Define a function that accepts a variable number of input arguments using varargin.
🌐
MathWorks
mathworks.com › matlab › programming › functions › argument definitions
varargout - Variable-length output argument list - MATLAB
If you use varargout to define ... a C/C++ function with a fixed number of output arguments. You specify the number of arguments at the time of code generation. See Specify Number of Input or Output Arguments to Entry-Point Functions (MATLAB Coder)....
🌐
MathWorks
mathworks.com › matlab › programming › functions › argument definitions
Checking Number of Arguments in Nested Functions - MATLAB & Simulink
This topic explains special considerations for using varargin, varargout, nargin, and nargout with nested functions. varargin and varargout allow you to create functions that accept variable numbers of input or output arguments. Although varargin and varargout look like function names, they ...