필터 지우기
필터 지우기

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

추가 답변 (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