Load all fields of a struct as input into a function

2 次查看(过去 30 天)
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.

采纳的回答

Akira Agata
Akira Agata 2019-2-19
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Cell Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by