2

Why am I getting an error:

??? Error using ==> ensureCellType Too many input arguments.

Error in ==> usage_dynamicVariableNaming at 11 result = dataHolder.ensureCellType(str);

when I am passing in right number of parameters?

% USAGE:

clear all;
clc;

elementNames = {'area_12345[<>]6789', 'apollo123', 'guruX', 'ok'};
elementTypes = {'string', 'specialChar', 'int', 'float'};
elementValues = {'charlie', 'vvv', '09', '123.321'};

dataHolder = dynamicVariableNaming;

str = 'test';
result = dataHolder.ensureCellType(str);


%% CLASS
classdef dynamicVariableNaming
%HELLO Summary of this class goes here
%   - 

   properties           
           variableNames = [];           

           variableValues = [];
           variableTypes = [];
   end

   methods (Access = public) % (Access = private)
           function obj = dynamicVariableNaming (variableName, variableValue, variableType)
           % class constructor
               if(nargin > 0)
                 obj.variableNames = variableName;                 

                 obj.variableValues = variableValue;
                 obj.variableTypes = variableType;
               end
           end  
%    end
%            
%    methods (Static = true)
           function addVariables (obj, variableName, variableValue, variableType)
                 obj.variableNames = [obj.variableNames ensureCellType(variableName)];                 

                 obj.variableValues = [obj.variableValues ensureCellType(variableValue)];
                 obj.variableTypes = [obj.variableTypes ensureCellType(variableType)];
           end               

           function cellData = ensureCellType(value)       
            if (class(value) ~= 'cell') 
                cellData = cell2string(value);
            else
                cellData = value;
            end
           end            

   end   
end 

Thanks for your great help. It now runs, but the data is not inserted.

I started a new thread on this new issue: The data is not inserted successfully into object

Community
  • 1
  • 1
Ursa Major
  • 851
  • 7
  • 25
  • 47

1 Answers1

6

Unless you intend ensureCellType to be a Static method (in which case you should declare it with (Static=true), you should give it the signature cellData = ensureCellType(obj,value). obj gives you a reference to the object itself within the method.

You're getting the error you see because MATLAB is passing in both the object itself and value into your method, which is two input arguments rather than one.

If you will never need a reference to obj within the method, you can declare the method signature as cellData = ensureCellType(~, value). Then MATLAB will know that it should have two inputs, but it can ignore passing in the first.

Sam Roberts
  • 23,951
  • 1
  • 40
  • 64
  • 2
    addVariables() is still giving errors. ====================================================== ??? Error using ==> addVariables Too many input arguments. Error in ==> usage_dynamicVariableNaming at 14 dataHolder.addVariables(dataHolder, elementNames(i), elementTypes(i), elementValues(i)); – Ursa Major Jan 08 '13 at 09:40
  • 1
    You need to define your method signatures as `output = methodName(obj, otherInputs)`, but you need to call your methods as `output = obj.methodName(otherInputs)`. `obj` is passed in _implicitly_ as the first input argument - if you pass it in directly, there will be an extra input argument giving you the error you see. – Sam Roberts Jan 08 '13 at 11:02