calling a function with struct inputs
1 view (last 30 days)
Show older comments
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 Comment
Stephen23
on 13 Feb 2024
Moved: Stephen23
on 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.
Accepted Answer
Pooja Kumari
on 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)
0 Comments
More Answers (0)
See Also
Categories
Find more on Argument Definitions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!