which way to call function is better?
Show older comments
I have more than hundred of input parameters in myfunc and I am using the following way to call the function after grouping the parameters in the cell array C.
C = {a,b,c};
d = myfunc(C{:});
function d = myfunc(a,b,c)
d = a * b + c;
end
I am wondering wether the following way is better or not
C = {a,b,c};
d = myfunc(C);
function d = myfunc(C)
[a,b,c] = C{:};
d = a * b + c;
end
2 Comments
"which way to call function is better?"
Neither: using positional input arguments with "more than hundred" parameters is madness.
Just use a scalar structure. Within the function access the fields of the structure directly (i.e. do NOT try to allocate the field values to individual variables). A scalar structure of parameters = more robust, easier to work with, easier to debug, fewer chances of mistakes.
Accepted Answer
More Answers (1)
Matt J
on 7 Sep 2019
I have more than hundred of input parameters in myfunc and I am using the following way to call the function after grouping the parameters in the cell array C.
Are the parameters a,b,c, all scalars? If so, you should be carrying them around and passing them to functions as a vector
v=[a,b,c,...]
not as a cell array or struct.
Categories
Find more on Structures in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!