calling a function with struct inputs

4 visualizaciones (últimos 30 días)
Zahraa
Zahraa el 13 de Feb. de 2024
Movida: Stephen23 el 13 de Feb. de 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 comentario
Stephen23
Stephen23 el 13 de Feb. de 2024
Movida: Stephen23 el 13 de Feb. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

Pooja Kumari
Pooja Kumari el 13 de Feb. de 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)

Más respuestas (0)

Categorías

Más información sobre Programming Utilities en Help Center y File Exchange.

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by