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

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

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

Thanks. It worked out
@Akram Awad: don't forget to accept TADA's answer.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2019년 1월 6일

댓글:

2019년 1월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by