1

The data is not inserted successfully.

Output:

dataHolder.variableNames =      []

when it should be :

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 
Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122
Ursa Major
  • 851
  • 7
  • 25
  • 47

1 Answers1

2

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.

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

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

KlausCPH
  • 1,816
  • 11
  • 14
  • You did it, KlausCPH ! Wow. Pardon, I am new to Matlab OO. The syntax has several differences to C#, Java, etc. I am extremely grateful. Thank you, KlausCPH. – Ursa Major Jan 08 '13 at 10:43
  • 1
    Glad to help. This difference trips almost everybody new to Matlab OO. Mathworks knows it and new versions of Matlab do therefore warn about this actually. – KlausCPH Jan 08 '13 at 11:16