1

数据未插入成功。

输出:

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 
4

1 回答 1

2

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

要修复它,要么让你的班级继承班级,要么从班级handle返回objaddVariables

不能说代码中是否还有其他问题,因为它的格式很差,无法在matlab中运行(两端不平衡,缺少构造函数等)

于 2013-01-08T10:39:54.807 回答