Object Oriented MATLAB

Posted Fri, Feb 20 2009 5:39 PM by amarsan

I'm really happy to finally be trying out MATLAB's objected oriented features that first appeared in release 2008a. What a treat to finally be able to prototype algorithms and write tools for clients using a paradigm that comes very naturally to so many software developers. Also, it's a lot easier to bridge the gap between scientific and engineering algorithm inventors and software developers when each party's code looks similar to the other's.

Here are some introductions to object oriented MATALB:

Information from The MathWorks

An article by a MATLAB user 

Here's a sample class that I assembled today from several scripts that I have for a single project. The Apply, Plot, and CreateFromConstantValueSignalFiles methods had previously been standalone .m files. Now they are grouped together as they should be in the PolyTransform class. This makes me so much happier! Thanks, MATLAB!!

classdef PolyTransform

    properties
        coeffs
        degree
    end

    methods

        % Constructor
        function obj = PolyTransform(degree, coeffs)
            obj.degree = degree;
            obj.coeffs = coeffs;
        end

        % Other methods
        function signal2 = Apply(obj, signal)
            signal2 = Signal(signal.time, polyval(obj.coeffs, signal.response));
        end

        function Plot(obj, xx, yy)
            testx = min(xx):0.01:max(xx);
            testy = polyval(obj.coeffs, testx);
            figure();
            cla
            hold on
            plot(xx, yy, 'bo');
            plot(testx, testy, 'g-');
        end

    end

    methods (Static)

        function obj = CreateFromConstantValueSignalFiles(degree, ndiameters, diameters, basicFileName)

            averageValue = zeros(degree, 1);

            for ii = 1:ndiameters
                % Load in noise file
                fileName = strcat(basicFileName, num2str(ii-1), '.bin');
                signals = Signal.LoadFromNoiseFile(fileName);

                % Get average value
                averageValue(ii) = signals(1).Average();
            end

            coeffs = polyfit(averageValue, diameters, degree);

            obj = PolyTransform(degree, coeffs);
            obj.Plot(averageValue, diameters);

        end
    end

end
Filed under:

Comments

# re: Object Oriented MATLAB

Tuesday, February 24, 2009 7:46 AM by Patrick Steele

Can these classes be exposed to .NET via MATLAB's .NET Builder?

# re: Object Oriented MATLAB

Tuesday, February 24, 2009 10:53 PM by Anne

Good question - I'll have to look into this. My guess is that they can be.