Azzera filtri
Azzera filtri

calling a function with struct inputs

4 visualizzazioni (ultimi 30 giorni)
Zahraa
Zahraa il 13 Feb 2024
Spostato: Stephen23 il 13 Feb 2024
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 Commento
Stephen23
Stephen23 il 13 Feb 2024
Spostato: Stephen23 il 13 Feb 2024
"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.

Accedi per commentare.

Risposta accettata

Pooja Kumari
Pooja Kumari il 13 Feb 2024
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)

Più risposte (0)

Categorie

Scopri di più su Programming Utilities in Help Center e File Exchange

Prodotti


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by