luftfahrt
luftfahrt

Reputation: 5

Matlab - Constructor doesn't initliaize member values

Dear lovely community,

I already looked in Google and in the forum and also found some interesting posts. But in the end I still didn't get it worked. So, I am going to post the question here. I know in Matlab are already get/ Set methods implemented, but I am using objects and therefore I didn't understand where to implement them.

Overall Structure:

+Measurement\MeasurerIF
+Measurement\MeasurerComponent

In my Interface I declare my functions I want to implement and this Interface is furthermore abstract. It looks like this:

classdef MeasuererIF < handle

methods (Abstract=true)

    getStatus(cObj) ;
    setStatus(cObj,iStatus) ;
    getInfo(cObj) ;
    setInfo(cObj,sInfo) ;
end

end

Then I created the other class MeasurerComponent in which I implemented the methods and also the constructor:

classdef MeasurerComponent < PerformanceMeasurement.MeasuererIF
%% MeasurerComponent: Evaluates which model is used and contains them as childs.
%   Detailed explanation goes here

properties (Access=protected)
    miStatus;
    msInfo;
    mcData;
end

methods 

    %constructor
    function cObj = PerformanceMeasurement.MeasurerComponent ;

        cObj.miStatus = -1 ;
        cObj.msInfo = 'Import' ;
        cObj.mcData = [] ;
    end


    %Status
    function setStatus(cObj,iStatus)

        cObj.miStatus = iStatus;
    end

    function iStatus = getStatus(cObj)

        iStatus = cObj.miStatus;
    end

    %Info
    function setInfo(cObj,sInfo)

        cObj.msInfo = sInfo;
    end

    function sInfo = getInfo(cObj)

        sInfo = cObj.msInfo ;
    end

end

end

Earlier I just used the getMethods and recently added the setMethods and now it doesn't work anymore. The Problem is that when I create an object

obj = Measurement.MeasurerComponent

the programm doesn't initialize the values anymore.

For a = obj.getInfo;

I receive just []

Does someone has an idea why it doesn't get initialized anymore? After I received a tip I changed the initialization process to the constructor due to the handle class.

I am really thankful and glad about each hint and tip! Cheers

Upvotes: 0

Views: 69

Answers (1)

Tom
Tom

Reputation: 1125

Your constructor should be defined by function cObj = MeasurerComponent, without the PerformanceMeasurement prefix. This is just the way that packages are defined and used in Matlab - you add the prefix if using the class from outside the package, but not within the package (explained here: "Note that definitions do not use the package prefix" - http://uk.mathworks.com/help/matlab/matlab_oop/scoping-classes-with-packages.html?refresh=true).

Also, you have a typo - your abstract parent class file is called +Measurement\MeasurerIF but in the definition you spell it MeasuererIF. It doesn't matter which one it is called, but the name of the class does have to match the filename!

If I correct both of these issues, then your code becomes

+Measurement\MeasurerIF

classdef MeasurerIF < handle

    methods (Abstract=true)

        getStatus(cObj)
        setStatus(cObj,iStatus)
        getInfo(cObj)
        setInfo(cObj,sInfo)
    end
end  

+Measurement\MeasurerIF

classdef MeasurerComponent < Measurement.MeasurerIF
    %% MeasurerComponent: Evaluates which model is used and contains them 
    %% as childs.

        properties (Access=protected)
        miStatus
        msInfo
        mcData
    end

    methods

        %constructor
        function cObj = MeasurerComponent

            cObj.miStatus = -1 ;
            cObj.msInfo = 'Import' ;
            cObj.mcData = [] ;
        end


        %Status
        function setStatus(cObj,iStatus)

            cObj.miStatus = iStatus;
        end

        function iStatus = getStatus(cObj)

            iStatus = cObj.miStatus;
        end

        %Info
        function setInfo(cObj,sInfo)

            cObj.msInfo = sInfo;
        end

        function sInfo = getInfo(cObj)

            sInfo = cObj.msInfo ;
        end

    end
end

and if I type the following:

obj = Measurement.MeasurerComponent;
obj.getInfo

then I get back

ans =

Import

which is what I expect.

Upvotes: 2

Related Questions