Load all fields of a struct as input into a function

1 回表示 (過去 30 日間)
Max Bornemann
Max Bornemann 2019 年 2 月 19 日
コメント済み: Max Bornemann 2019 年 2 月 19 日
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
  1 件のコメント
Max Bornemann
Max Bornemann 2019 年 2 月 19 日
Thank you. Exactly what i was searching for.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by