calling a function with struct inputs

1 回表示 (過去 30 日間)
Zahraa
Zahraa 2024 年 2 月 13 日
移動済み: Stephen23 2024 年 2 月 13 日
Hi,
How can I call such a function:
function varargout = hey(init,varargin)
if init ==1
if nargin >1
geom_gs = varargin{1};
m = geom_gs.box.m;
n = geom_gs.box.n;
Lx = geom_gs.box.Lx;
Ly = geom_gs.box.Ly;
end
end
end
  1 件のコメント
Stephen23
Stephen23 2024 年 2 月 13 日
移動済み: Stephen23 2024 年 2 月 13 日
"How can I call such a function"
Because it neither displays anything nor returns any output it is not a very interesting function to call.
But lets call it anyay:
S.box.m = 1;
S.box.n = 2;
S.box.Lx = 3;
S.box.Ly = 4;
hey(1,S)
function varargout = hey(init,varargin)
if init ==1
if nargin >1
geom_gs = varargin{1};
m = geom_gs.box.m;
n = geom_gs.box.n;
Lx = geom_gs.box.Lx;
Ly = geom_gs.box.Ly;
end
end
end
Note that VARARGIN can be replaced by a simple variable name.
Note that VARARGOUT is unused and can be removed.

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

採用された回答

Pooja Kumari
Pooja Kumari 2024 年 2 月 13 日
Hi,
To call the hey function, you would use the following syntax in MATLAB:
initValue = 1;
geom_gs.box.m = 10;
geom_gs.box.n = 20;
geom_gs.box.Lx = 100;
geom_gs.box.Ly = 200;
hey(initValue, geom_gs)
You have not returned anything from the "hey" function, so you can change the function and return the values to the "varargout"
You can refer below for the updated code:
function varargout = hey(init,varargin)
if init == 1
if nargin > 1
geom_gs = varargin{1};
m = geom_gs.box.m;
n = geom_gs.box.n;
Lx = geom_gs.box.Lx;
Ly = geom_gs.box.Ly;
% Assign to varargout
varargout{1} = m;
varargout{2} = n;
varargout{3} = Lx;
varargout{4} = Ly;
end
end
end
To call the hey function, you would use the following syntax in MATLAB:
[m,n,Lx,Ly] = hey(initValue, geom_gs)

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeArgument Definitions についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by