Load all fields of a struct as input into a function

1 vue (au cours des 30 derniers jours)
Max Bornemann
Max Bornemann le 19 Fév 2019
Commenté : Max Bornemann le 19 Fév 2019
Hello! I´m new in Matlab. I'm searching for an easy way to load all the fields of a struct as input into a function.
What i want to do:
Function:
function [Var3] = TestFunction(Input)
Var3=Var1*Var2;
end
Script:
Input.Var1=5;
Input.Var2=6;
[Var3] = TestFunktion(Input);
Error: Undefined function or variable 'Var1'.
Workaround would be:
Function:
function [Var3] = TestFunction(Var1 Var2)
Var3=Var1*Var2;
end
Script:
Input.Var1=5;
Input.Var2=6;
[Var3] = TestFunktion(Input.Var1 Input.Var2);
But imagine if you have like 100 fields inside the Input-struct and you need all of them as input for your function, it would take a long time to write down all the Input-fields separately.
I will greatly appreciate any assistance.

Réponse acceptée

Akira Agata
Akira Agata le 19 Fév 2019
If your structure Input always has fields Var1 and Var2, a simple solution would be like this.
function Var3 = TestFunction(Input)
Var3 = Input.Var1*Input.Var2;
end
  1 commentaire
Max Bornemann
Max Bornemann le 19 Fév 2019
Thank you. Exactly what i was searching for.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Cell Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by