Why do I get a “Too many input arguments” error when passing in proper parameters?

一世执手 提交于 2019-12-19 10:16:29

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/14196336/why-do-i-get-a-too-many-input-arguments-error-when-passing-in-proper-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!