数据未成功插入对象

时间:2013-01-08 10:11:58

标签: oop matlab

未成功插入数据。

输出:

dataHolder.variableNames =      []

应该是:

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

%USAGE:

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 

1 个答案:

答案 0 :(得分:2)

当您使用非addVariables对象时,您不会根据需要从handle方法返回已更改的对象。请记住,与其他基于参考传递的语言相比,matlab是不同的。

要解决此问题,请将您的班级设为handle班级的enherit,或者从obj

返回addVariables

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