Félix Veysseyre
Félix Veysseyre

Reputation: 259

Can not set parameter of Matlab class

I have created a (very first) Matlab class to store images sequences.

When I applied method to an instance of the class, the attributs of the class are not set at all.

classdef sequence

    %% Properties %%

    properties
        images;
        width;
        height;
    end

    %% Methods %%

    methods

        %% Constuctor %%

        function obj = sequence()
            obj.images = {};
            obj.width = -1;
            obj.height = -1;
        end

        %% Others methods %%

        function numberOfImages = getNumberOfImages(obj)
           numberOfImages = length(obj.images); 
        end

        function addImage(obj, imageToAdd)

            numberOfImages = obj.getNumberOfImages();

            obj.images{numberOfImages + 1} = imageToAdd;

            if numberOfImages == 0
                [h, w] = size(imageToAdd);

                obj.height = h;
                obj.width = w;
            end

        end

        function image = getImage(obj, i)
           image = obj.images{i}; 
        end

    end

end

I followed Matworks documentation carefully, but I still do not know where is my mistake.

Here is the code I wrote to use my class :

%% Parameters %%

imageFilename1 = '../Data/Test/1.png';
imageFilename2 = '../Data/Test/2.png';

alpha = 50;
numberOfIterations = 50;

%% Read images %%

image1 = double(imread(imageFilename1));
image2 = double(imread(imageFilename2));

imageSequence = sequence();
imageSequence.addImage(image1);
imageSequence.addImage(image2);

Where I am wrong ?

Upvotes: 3

Views: 61

Answers (1)

Nick
Nick

Reputation: 3193

You are using a value class, therefore changes to parameters do not change the actual object. In order to make it work you should change the first line to:

classdef sequence < handle

This way you have created a handle class, which can be used as you want.

For more information you could check this page

Upvotes: 2

Related Questions