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

16 vues (au cours des 30 derniers jours)
Akram Awad
Akram Awad le 6 Jan 2019
Commenté : Stephen23 le 7 Jan 2019
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 commentaire
Stephen23
Stephen23 le 7 Jan 2019
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.

Connectez-vous pour commenter.

Réponses (1)

TADA
TADA le 6 Jan 2019
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 commentaires
Akram Awad
Akram Awad le 7 Jan 2019
Thanks. It worked out
Stephen23
Stephen23 le 7 Jan 2019
@Akram Awad: don't forget to accept TADA's answer.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by