传递适当的参数时,为什么会出现“输入参数过多"错误? [英] Why do I get a “Too many input arguments” error when passing in proper parameters?

查看:195
本文介绍了传递适当的参数时,为什么会出现“输入参数过多"错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么会出现错误:

???使用==> sureCellType时出错,输入参数过多.

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

==> usage_dynamicVariableNaming的错误,结果为11 = dataHolder.ensureCellType(str);

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

推荐答案

除非您打算将ensureCellType用作静态方法(在这种情况下,您应使用(Static=true)进行声明,但应为其赋予签名cellData = ensureCellType(obj,value) .obj为您提供了方法中对象本身的引用.

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.

您看到的错误是因为MATLAB会将对象本身和value都传入了方法中,该方法是两个输入参数,而不是一个.

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.

如果您永远不需要方法中对obj的引用,则可以将方法签名声明为cellData = ensureCellType(~, value).然后,MATLAB将知道它应该有两个输入,但是可以忽略第一个输入.

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.

这篇关于传递适当的参数时,为什么会出现“输入参数过多"错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆