How to use varargin and varargout?

74 次查看(过去 30 天)
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!

采纳的回答

Stephen23
Stephen23 2020-7-7
编辑:Stephen23 2020-7-7
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}};

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Argument Definitions 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by