How to use varargin and varargout?

71 visualizaciones (últimos 30 días)
Roxanne Esguerra
Roxanne Esguerra el 7 de Jul. de 2020
Comentada: Roxanne Esguerra el 12 de Jul. de 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!

Respuesta aceptada

Stephen23
Stephen23 el 7 de Jul. de 2020
Editada: Stephen23 el 7 de Jul. de 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}};

Más respuestas (0)

Categorías

Más información sobre Argument Definitions en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by