I want to improve clarity of my program. I have 20+ functions and majority of their inputs are the same. Can I store them in 1 object? Or should I use global variables instead?
Passing multiple variables to function in 1 input
18 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Maciej Grybko
am 4 Nov. 2018
Kommentiert: Maciej Grybko
am 6 Nov. 2018
Is there any way to pass multiple variables in a single structure/cell/vector/whatever to a function, so that I can avoid writing out all variables and call variables within function shortly by their names (rather than eg. structureName.variableName)?
In other words, instead of this:
a=1;
b=2;
c=3;
...
z=24;
myFun(a, b, c, ..., z)
(and inside my function:)
function myFun(a, b, c, ..., z)
newVal = c % calling variable shortly by its name
I would like to have something like:
alphabet = struct('a', 1, 'b', 2, 'c', 3, ..., 'z', 24);
myFun(alphabet)
(and inside my function:)
function myFun(alphabet)
newVal = c % calling variable shortly by its name
3 Kommentare
Stephen23
am 5 Nov. 2018
"Or should I use global variables instead?"
No.
Global variables are one way that beginners force themselves into writing slow, complex, buggy code.
Akzeptierte Antwort
Weitere Antworten (2)
Siehe auch
Kategorien
Mehr zu Variables finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!