image thumbnail
from Simple Object Creation in Matlab Object Oriented Programming (OOP) by Michael Chan
The objective is to provide a simple sample code to illustrate OOP in Matlab.

dynamicVariableNaming
classdef dynamicVariableNaming < handle
% dynamicVariableNaming
%   - part of an interpretor simulator for collecting variable data

   properties           
           variableNames = [];           
           
           variableValues = [];
           variableTypes = [];
   end

   methods (Access = public) % (Access = private)
           % class constructor
           function obj = dynamicVariableNaming (variableName, variableValue, variableType)
           
               if(nargin > 0)
                 obj.variableNames = ensureCellType(obj, variableName);                 
                 
                 obj.variableValues = ensureCellType(obj, variableValue);
                 obj.variableTypes = ensureCellType(obj, variableType);
               end
           end  

        % helper methods
           function obj = 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};
            else
                cellData = value;
            end
           end            
           
   end   
end 

        
           

Contact us