0

I'm writing a code using Matlab OO programming. I came across a problem trying to write a method that changes the value of a matrix inside the same object. I've tested my method and it does exactly what I want it to do but it does not save back the results.

Here is my code: Take a look on : function obj = initCC(obj)

%% searchDat picks values for the object properties in a fite %% Importa a função searchDat() %%

classdef methExpl
    %Classe de Metodos Explicitos
    %   Detailed explanation goes here

    properties
        %Declara Altura da Placa, Largura, Nós em X, Nós em Y, K, Matriz
        %com a solução do passo atual, matriz com a solução do passo

        %Variáveis Geométricas
        %Altura e Largura
        Height
        Length
        %Variáveis da Malha
        %Número de nós em X e Número de nós em Y
        %Passo de tempo
        NodeX
        NodeY
        timeStep
        %Propriedades Físicas
        %Condutividade Térmica, Temperatura no Contorno
        %Temperatura Inicial uniforme na chapa

        kTerm
        boundaryTemp
        initTemp

        %Soluções parciais
        %Matriz com a solução da iteração atual
        %Matriz com a solução da iteração passada
        curMat
        lastMat

    end

    properties (SetAccess = private)
        %Arma
        funTermMAT
        erro = {}
    end

    methods
        function obj = methExpl()
            %Construtor da Classe
            %Inicializa as variaveis com os valores no arquivo input.dat
            %Inicializa a matriz 
            obj.Height = searchDat('[Height]','input.dat');
            obj.Length = searchDat('[Length]','input.dat');
            obj.NodeX = searchDat('[NodesX]','input.dat');
            obj.NodeY = searchDat('[NodesY]','input.dat');
            obj.kTerm = searchDat('[ThermalConductivity]','input.dat');
            obj.boundaryTemp = searchDat('[BoundaryTemperature]','input.dat');
            obj.initTemp = searchDat('[InitalTemperature]','input.dat');
            obj.curMat = zeros(obj.NodeX,obj.NodeY);
            %inicializa a matriz com a temperatura do contorno:
            obj.initCC();
            obj.lastMat = zeros(obj.NodeX,obj.NodeY);
        end



        function obj = initCC(obj)
        %initCC Inicializa a matriz com a condição de contorno de
        %temperatura
            lim = size(obj.curMat);
            for (i =1 : lim(1))
                for (j = 1 : lim(2))
                    if (i==1) || (i == lim(1))
                        obj.curMat(i,j) = obj.boundaryTemp;   
                    elseif (j==1) || (j ==lim(2))
                        obj.curMat(i,j) = obj.boundaryTemp
                    end

                end
            end
            obj.curMat
        end



    end

end

Before quitting the initCC and get:

ans =

     3     3     3     3     3     3     3     3     3     3     3
     3     0     0     0     0     0     0     0     0     0     3
     3     0     0     0     0     0     0     0     0     0     3
     3     0     0     0     0     0     0     0     0     0     3
     3     0     0     0     0     0     0     0     0     0     3
     3     0     0     0     0     0     0     0     0     0     3
     3     0     0     0     0     0     0     0     0     0     3
     3     0     0     0     0     0     0     0     0     0     3
     3     0     0     0     0     0     0     0     0     0     3
     3     0     0     0     0     0     0     0     0     0     3
     3     3     3     3     3     3     3     3     3     3     3

Which is exactly what I want. If I call it from the outside after initializing it I get:

ans =

     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0

As if the result of the method I have created has been destroyed.

Artur Castiel
  • 185
  • 1
  • 15
  • 1
    When you call a method on a value class you need to store its output on the obj instance. Instead of obj.initCC() you need to use obj = obj.initCC(); See http://stackoverflow.com/questions/8086765/why-do-properties-not-take-on-a-new-value-from-class-method – Navan Oct 07 '15 at 13:41
  • 1
    Your class is a value class. For value classes, their methods copies `self` to a new object, modifies the new object, and return the new object. You need `new=0ld.initCC()` to get the result of the method. – user3528438 Oct 07 '15 at 13:42
  • 1
    However you can also change value class to handle class. For handle classes, methods modifies the `self` object directly and save the result back it `self`. – user3528438 Oct 07 '15 at 13:44
  • Is there any way my function can change the values within the object without having to take copying it back ? My object is going to be huge and a coppying operation is exactly what I'm trying to avoid. – Artur Castiel Oct 07 '15 at 13:47
  • 1
    http://www.mathworks.com/help/matlab/matlab_oop/comparing-handle-and-value-classes.html – user3528438 Oct 07 '15 at 13:55

1 Answers1

0

Just to put expand on the suggestions in the comments, you basically have two options. Firstly, you could simply capture the return of your initCC method which does correctly return the modified object, like so:

myObj = initCC(myObj);

Note that because of MATLAB's "copy on write" behaviour, this does not in fact incur any expensive copies of data.

The other option is to declare your class to be a handle, like so:

classdef methExpl < handle
...
end

then you don't need the return from initCC.

Edric
  • 23,676
  • 2
  • 38
  • 40