which way to call function is better?

2 Ansichten (letzte 30 Tage)
G A
G A am 5 Sep. 2019
Kommentiert: G A am 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.
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 Kommentare
Stephen23
Stephen23 am 5 Sep. 2019
Bearbeitet: Stephen23 am 5 Sep. 2019
"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.
G A
G A am 5 Sep. 2019
Bearbeitet: G A am 5 Sep. 2019
Thanks Stephen!
I have started to do changes in my code following your advise.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

John D'Errico
John D'Errico am 5 Sep. 2019
Bearbeitet: John D'Errico am 5 Sep. 2019
Neither way is "better". Both feel just a little clumsy, since I'm not sure why you feel the need to package the variables together in a cell array at all.
The first form seems a bit cleaner in my eye, perhaps closer to traditional MATLAB.
The latter form seems like it does not require the function to have a fixed number of input arguments. So arguably, there could be some elegance in the second one too. But then, the very first thing you needed to do was to unpack the arguments [a,b,c] into a fixed set of variables. So any elegance that seemed to be there is imediately gone.
Neither method will be more efficient. In both cases, you create a cell array, then unpack the cell array into a comma separated list via C{:}. So you're doing exactly the same thing, except that in one case, the comma separated list happens outside the function call, and in the other, the list creation happens inside the function.
Again, as I said, both forms seem a bit of a clumsy way to code. If you absolutely feel the need to use one of them, pick the one that makes you more happy.
Edit: I see that you have a hundred or so various parameters in your function, so you don't want to have a function call with a hundred input arguments. This seems to make some sense, that you don't want an extensive argument list.
In that case, a common approach is to pack the elements into a structure. Now you can access them directly as S.a, S.b, S.c, never needing to unpack them at all. But that makes more typing when you write your function, since you would need to attach the characters S. in front of each variable.
Of course, you could use tools like struct2cell to automatically unpack the struct. But that begs the question of why you wanted to created the struct as a package for your variables at all.
So, since you have a huge number of arguments, perhaps the second way seems simplest in the end. It allows you to not need to write a function header with that huge list of arguments.
  11 Kommentare
Bruno Luong
Bruno Luong am 7 Sep. 2019
There shouldn't be any significant difference between func1 and func2. MATLAB duplicates the structure field pointers when a/b change (but not the fields that have not been modified). In one case (func2) it's carried out inside the function, in other case (func1) outside juste after function returns. You shouldn't worry at this level and let MATLAB does the work, it's usually efficient.
G A
G A am 7 Sep. 2019
Thanks!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Matt J
Matt J am 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.

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by