calling a function with struct inputs

4 vues (au cours des 30 derniers jours)
Zahraa
Zahraa le 13 Fév 2024
Déplacé(e) : Stephen23 le 13 Fév 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 commentaire
Stephen23
Stephen23 le 13 Fév 2024
Déplacé(e) : Stephen23 le 13 Fév 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.

Connectez-vous pour commenter.

Réponse acceptée

Pooja Kumari
Pooja Kumari le 13 Fév 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)

Plus de réponses (0)

Catégories

En savoir plus sur Programming Utilities dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by