Undefined function 'plus' for input arguments of type 'cell'.

17 ビュー (過去 30 日間)
Akram Awad
Akram Awad 2019 年 1 月 6 日
コメント済み: Stephen23 2019 年 1 月 7 日
Helllo
I'm running the following code, when I don't enter values it works fine and returns the default values. but when I enter inputs (i.e main(2,3)), It gives me the following error: Undefined function 'plus' for input arguments of type 'cell'.It points to the last statement. Thanks in advance.
function out = main (varargin)
switch nargin
case 0
disp('no inputs entered');
a=1; % default values
b=1;
case 1
disp ('one input intered')
a= varargin(1);
b=1;
case 2
disp('two input entered ')
a=varargin(1);
b= varargin(2);
otherwise
error('maximum length is two for the input argument')
end
out = a+b;
  1 件のコメント
Stephen23
Stephen23 2019 年 1 月 7 日
You need to learn the two different ways to index cell arrays:
  • {} curly braces refer to the cell contents, whereas
  • () parentheses refer to the cells themselves.

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

回答 (1 件)

TADA
TADA 2019 年 1 月 6 日
varargin is a cell array
when you index a cell array using parentheses you get a cell array back, i.e:
c = {1 2 3};
a = c(1)
a =
1×1 cell array
{[1]}
instead you need to use curly bracers to index varargin:
a= varargin{1};
or in your case, simply don't use varargin, and use two input arguments instead:
function out = main (a, b)
switch nargin
case 0
disp('no inputs entered');
a=1; % default values
b=1;
case 1
disp ('one input intered');
b=1;
case 2
disp('two input entered ');
% this otherwise statement is no longer necessary,
% a builtin error will be thrown if too many arguments are sent to this function
%otherwise
% error('maximum length is two for the input argument')
end
out = a+b;
end
  2 件のコメント
Akram Awad
Akram Awad 2019 年 1 月 7 日
Thanks. It worked out
Stephen23
Stephen23 2019 年 1 月 7 日
@Akram Awad: don't forget to accept TADA's answer.

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by