1
votes

I am doing a simple test with OOP (Object Oriented Programming) in MATLAB. I made a simple class including a constructor. Additionally, I made three sections regarding properties: three regular properties; one constant; and three dependent properties. I included a couple of get/set methods for regular properties, one output method, and one protected / Static method that is used by the Constructor: 'getPropertyValue(propertyName, inputCell).

The Static method causes a problem. I am sending two parameters: 'propertyName' of string type, and 'inputCell' of cell type. The last argument is supposed to send all input parameters copied from varargin defined in the Constructor.

When I try to use the class and make its instance in the Command Line,

a = FigureObjects('promien',10)

I get this error:

Undefined function 'getPropertyValue' for input arguments of type 'cell'.

Error in FigureObjects (line 26) [temp,status,msgStatus] = getPropertyValue('podstawa',inputCell);

I though I could send anything to any function such as string, cell, matrix etc. Is there any way to send a cell to this function and read its content?

Thanks

PS. This is my code:

classdef FigureObjects
%FIGUREOBJECTS Summary of this class goes here
%   Detailed explanation goes here

properties
    podstawa
    wysokosc
    promien
end

properties (Constant = true, Hidden = true)
    liczbaPi = 3.14
end

properties (Dependent = true)
    obwodKola
    obwodProstokata
    obwodKwadratu
end

methods
    % Constructor
    function obj = FigureObjects(varargin)
        inputCell = varargin;
        if nargin > 0
            [temp,status,msgStatus] = getPropertyValue('podstawa',inputCell);
            if status
                obj.podstawa = temp;
            else
                error(msgStatus);
            end
            [temp,status,msgStatus] = getPropertyValue('wysokosc',inputCell);
            if status
                obj.wysokosc = temp;
            else
                error(msgStatus);
            end
            [temp,status,msgStatus] = getPropertyValue('promien',inputCell);
            if status
                obj.promien = temp;
            else
                error(msgStatus);
            end
        end
    end

    % get functions
    function obwodKola = get.obwodKola(obj)
        if ~isempty(obj.promien)
            obwodKola = 2*FigureObjects.liczbaPi*obj.promien;
        else
            error('''promien'' is not defined.');
        end
    end
    function obwodProstokata = get.obwodProstokata(obj)
        if (~isempty(obj.podstawa) && ~isempty(obj.wysokosc))
            obwodProstokata = 2*(obj.podstawa*obj + obj.wysokosc);
        else
            error('Either ''podstawa'' or/and ''wysokosc'' are not defined.');
        end
    end
    function obwodKwadratu = get.obwodKwadratu(obj)
        if ~isempty(obj.podstawa)
            obwodKwadratu = 4*obj.podstawa;
        else
            error('''podstawa'' is not defined.');
        end
    end
    function promien = get.promien(obj)
        promien = obj.promien;
    end
    function podstawa = get.podstawa(obj)
        podstawa = obj.podstawa;
    end
    function wysokosc = get.wysokosc(obj)
        wysokosc = obj.wysokosc;
    end

    % set functions
    function obj = set.promien(obj,promien)
        if isnumeric(promien)
            obj.promien = promien;
        else
            error('''promien'' must be numeric.');
        end
    end
    function obj = set.podstawa(obj,podstawa)
        if isnumeric(promien)
            obj.podstawa = podstawa;
        else
            error('''podstawa'' must be numeric.');
        end
    end
    function obj = set.wysokosc(obj,wysokosc)
        if isnumeric(wysokosc)
            obj.wysokosc = wysokosc;
        else
            error('''wysokosc'' must be numeric.');
        end
    end

    % output functions
    function output = obwod(theFigureObj, rodzajFigury)
        if strcmpi(rodzajFigury,'kolo')
            output = theFigureObj.obwodKola;
        elseif strcmpi(rodzajFigury,'prostokat')
            output = theFigureObj.obwodProstokata;
        elseif strcmpi(rodzajFigury,'kwadrat')
            output = theFigureObj.obwodKwadratu;
        else
            error('Cannot identify a figure.');
        end
    end % obwod
end % methods

methods (Access = protected, Static)
    function [propertyValue, status, msgString] = ...
            getPropertyValue(propertyName, inputCell)
        I = strcmpi(propertyName,inputCell);
        if sum(I) ~= 0
            if (find(I)+1) <= length(inputCell)
                propertyValue = inputCell{find(I)+1};
                status = true;
                msgString = 'No errors';
            else
                propertyValue = [];
                status = false;
                msgString = ['Too few arguments for ''' propertyName '''.'];
            end
        else
            propertyValue = [];
            status = false;
            msgString = ['No property ' propertyName];
        end
    end % getPropertyValue(propertyName, cellArray)
end % methods (Access = protected, Static)

end % classdef

1

1 Answers

7
votes

Just because the method is static, doesn't mean you can use it as if it were a separate function in the search path. You need to call obj.getPropertyValue or FigureObjects.getPropertyValue instead.