Mix Database

by

 

Use database-schemes in Matlab for more speed

MixDatabase
classdef MixDatabase
    % MixDatabase shall bring SQL-features to Matlab and handle numeric
    % operations on database objects even quicker by avoiding unnecessary
    % type checking and conversion
    
    properties
        ordAr % uint32, sorts the database
        headAr % cell-array containing mixed, headline
        keyAr % integer determining the columns containing the keys
        boolAr % logical (1)
        floatAr % double (2)
        strAr % cell-array with chars (3)
        dbDim % integer 2x1 determining the dime [obj.headAr(:,k); obj.floatAr(:,floatind)]nsions
    end % properties
    
    methods
        function obj = MixDatabase(a,headRows,idCols)
            % MIXDATABASE creates a mixed Database out of a cell-Array
            %
            % MixDatabase splits a cell-array in columns with different
            % types, which is just similar to other SQL-like databases.
            % This results in much quicker statistics calculation and
            % database requests. No further conversions will be necessary.
            %
            % Input
            % 1 a: cell-Array
            % 2 headRows: selector for headlines in a
            % 3 idCols: selector for key-columns in a
            %
            % Output
            % 1 obj: a MixDatabase object
            %
            % See also LogDatabase
            %
            % Copyright 2011-2012 Vincent Hahn, Siemens AG
            %
            % $Revision: 0.0.1 $ $Date: 2011/12/27 15:37:09 $
            if iscell(a)
                obj.dbDim = size(a);
                obj.headAr = a(headRows,:);
                a(headRows,:) = [];
                obj.keyAr = idCols;
                for k = 1:numel(a(1,:))
                    if all(cellfun(@islogical,a(:,k)))
                        obj.ordAr(k) = 1;
                        obj.boolAr = [obj.boolAr logical(cell2mat(a(:,k)))];
                    elseif all(cellfun(@isnumeric,a(:,k)))
                        obj.ordAr(k) = 2;
                        obj.floatAr = [obj.floatAr cell2mat(a(:,k))];
                    else
                        obj.ordAr(k) = 3;
                        obj.strAr = [obj.strAr a(:,k)]; 
                    end
                end
            else
                error('MixDatabase:WrongParameterType','Input must be of type cell');
            end
        end % MixDatabase
        
        function b = selectColsFromDb(obj,varargin)
            %SELECTCOLSFROMDB selects certain columns in the database
            %
            % C = mixdb.selectColsFromDb(); returns in C the whole
            % database as a Cell-Array
            %
            % C = mixdb.selectColsFromDb(selType, selValue); returns in C
            % an Array depending on the selType-Selection (see below)
            %
            % This function is similar to the SQL-statement "SELECT
            % selValue(selType) FROM MixDatabase". It only provides the
            % possibility for one condition. Usually you may want to select
            % numeric values, which is just the big advantage of
            % MixDatabases.
            % This function is made to be improved in future versions
            % 
            % Input:
            % 1 selType: String or number of the below
            %   1: 'datatype'
            % 2 selValue: Value for the Type
            %   1: 'boolean' on selType = 1
            %   2: 'numeric' on selType = 1
            %   3: 'string' on selType = 1
            %
            % Output:
            % 1 b: Array of the desired type
            %
            % See also LogDatabase
            %
            % Copyright 2011-2012 Vincent Hahn, Siemens AG
            %
            % $Revision: 0.0.1 $ $Date: 2011/12/27 16:20:17 $
            
            b = cell(obj.dbDim);
            if numel(varargin) == 0
                boolind = 1;
                floatind = 1;
                strind = 1;
                for k = 1:numel(obj.ordAr)
                    if obj.ordAr(k) == 1
                        b(:,k) = [obj.headAr(:,k); num2cell(obj.boolAr(:,boolind))];
                        boolind = boolind+1;
                    elseif obj.ordAr(k) == 2
                        b(:,k) = [obj.headAr(:,k); num2cell(obj.floatAr(:,floatind))];
                        floatind = floatind + 1;
                    elseif obj.ordAr(k) == 3
                        b(:,k) = [obj.headAr(:,k); obj.strAr(:,strind)];
                        strind = strind + 1;
                    end
                end
            elseif numel(varargin) == 2
                if (isnumeric(varargin{1}) && varargin{1} == 1) || strcmp(varargin{1},'datatype')
                    if strcmp(varargin{2},'boolean')
                        varargin{2} = 1;
                    elseif strcmp(varargin{2},'float')
                        varargin{2} = 2;
                    elseif strcmp(varargin{2},'string')
                        varargin{2} = 3;
                    end
                    switch varargin{2}
                        case 1
                            cAr = obj.boolAr;
                        case 2
                            cAr = obj.floatAr;
                        case 3
                            cAr = obj.strAr;
                    end % switch
                    selAr = arrayfun(@(x) isequal(x,varargin{2}),obj.ordAr);
                    b = [obj.headAr(:,selAr);cAr];
                end % varargin{1} = 'datatype'
            end % if: numel(varargin)
        end
        
        function b = getFloatStatisticsFromDb(obj,type)
            %GETFLOATSTATISTICSFROMDB calculates statistics on floats.
            %
            % s = mixdb.getStatisticsFromDb(TYPE) does nearly the same as
            % s = TYPE(obj.selectColsFromDb(1,2)) (when no headline-rows
            % exist)
            %
            % Input:
            % 1 type: std, nanstd, mean, nanmean, median, nanmedian, min,
            % max
            %
            % Output:
            % 1 b: Cell-Array with statistics
            %
            % See also LogDatabase
            %
            % Copyright 2011-2012 Vincent Hahn, Siemens AG
            %
            % $Revision: 0.0.1 $ $Date: 2011/12/27 16:42:42 $
            b = num2cell(eval([type '(obj.floatAr)']));
            b = [obj.headAr(:,obj.ordAr == 2);b];
        end
        
        function b = getBoolStatisticsFromDb(obj,type)
            %GETSTATISTICSFROMDB calculates statistics on boolean-values.
            %
            % s = mixdb.getStatisticsFromDb(TYPE) does nearly the same as
            % s = TYPE(obj.selectColsFromDb(1,1)) (when no headline-rows
            % exist)
            %
            % Input:
            % 1 type: std, nanstd, mean, nanmean, median, nanmedian, min,
            % max
            %
            % Output:
            % 1 b: Cell-Array with statistics
            %
            % See also LogDatabase
            %
            % Copyright 2011-2012 Vincent Hahn, Siemens AG
            %
            % $Revision: 0.0.1 $ $Date: 2011/12/27 16:42:42 $
            b = num2cell(eval([type '(obj.boolAr)']));
            b = [obj.headAr(:,obj.ordAr == 1);b];
        end
    end % methods
    
end % classdef

Contact us