How to use varargin and varargout?

77 Ansichten (letzte 30 Tage)
Roxanne Esguerra
Roxanne Esguerra am 7 Jul. 2020
Kommentiert: Roxanne Esguerra am 12 Jul. 2020
Hi, my code is
function [A varargout] = rectangular(L,W,varargin)
A = L*W;
n = nargin;
if n==3
varargout = L*W*varargin;
end
end
When I call the function using 3 variables, I get an error:
>> [a v]=rectangular(2,3,4)
Undefined operator '*' for input arguments of type 'cell'.
Error in rectangular (line 6)
varargout = L*W*varargin;
What should be replaced to make the code work?
Thanks in advance!

Akzeptierte Antwort

Stephen23
Stephen23 am 7 Jul. 2020
Bearbeitet: Stephen23 am 7 Jul. 2020
As their documentation explains, both varargin and varargout are cell arrays. So if required (e.g. to perform numeric operations on their contents) you will need to use appropriate indexing to access their contents:
For your example code:
varargout{1} = L*W*varargin{1};
or slightly more robustly:
varargout = {L*W*varargin{1}};

Weitere Antworten (0)

Kategorien

Mehr zu Argument Definitions finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by