How to use varargin and varargout?

74 ビュー (過去 30 日間)
Roxanne Esguerra
Roxanne Esguerra 2020 年 7 月 7 日
コメント済み: Roxanne Esguerra 2020 年 7 月 12 日
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}};
  1 件のコメント
Roxanne Esguerra
Roxanne Esguerra 2020 年 7 月 12 日
Thank you, it worked!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeArgument Definitions についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by