数据未成功插入对象 [英] The data is not inserted successfully into object

查看:76
本文介绍了数据未成功插入对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据未成功插入.

输出:

dataHolder.variableNames =      []

应在何时:

dataHolder.variableNames = [{'area_12345[<>]6789'}, {'apollo123'}, {'guruX'}, {'ok'}];

%使用率:

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);


for i = 1:3
    dataHolder.addVariables(elementNames(i), elementTypes(i), elementValues(i));
end

dataHolder.variableNames

%%% 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(obj, variableName)];                 

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

           function cellData = ensureCellType(obj, value)   

            if (~strcmp(class(value), 'cell')) 
                cellData = {value};
                % cell2string(value); 
            else
                cellData = value;
            end
           end            

   end   
end 

推荐答案

在使用非handle对象时,没有按要求从addVariables方法返回更改的对象.请记住,与其他基于引用传递的语言相比,matlab有所不同.

You are not returning the changed opbject from the addVariables method as required when you are working with non-handle objects. Remember, matlab is different in comparison with other reference passing-based languages.

要解决此问题,请使您的类成为handle类的继承者,或者从addVariables

To fix it either make your class enherit from the handle class, or return obj from addVariables

不能说代码是否存在其他问题,原因是其格式不佳以及无法在matlab中运行(不平衡的末端,缺少的构造函数等)

Cant say if there are other problems in the code due to its poor formatting and inability to run in matlab (unbalanced ends, missing constructors, etc.)

这篇关于数据未成功插入对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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