1

After asking my previous question I have come to a situation where I use a couple of object classes stored in each other's properties to retain access to several fields and methods. For example

classdef Class1

    properties
        Class1Prop % A property accessible from Class1
        Class2     % A cell array of class 2 objects
    end

    methods
        % Construct the class with all of its properties
        function self = Class1()

        end

        function Class1Method
            self.Class1Prop = ...
        end
    end
end

I populate an object of Class1 that contains a cell array of Class2. Now I would like to have methods change the values of properties inside of this object. i.e.

    Class1{index}.Class2{index}.Class2Method 

Will perform some computation and now have that value stored somewhere in that instance of the class.

As stated in the matlab documentation:

"If a function modifies a handle object passed as an input argument, the modification affects the object referenced by both the original and copied handles."

To gain the functionality I want, I have to use value classes (with methods that return the class object) so that the value returned by the method call is changed. The value returned can be assigned as well:

Class1{index}.Class2{index} = Class1{index}.Class2{index}.Class2Method

However, ideally

Class1{index}.Class2{index}.Class2Method

would update the Class2 properties. And that is the functionality I want. Is this possible?

Community
  • 1
  • 1
St-Ste-Ste-Stephen
  • 1,076
  • 10
  • 19
  • sorry about all the edits, I think this might actually be a useful question now ;) – St-Ste-Ste-Stephen Jan 30 '12 at 06:48
  • **@St-Ste-Ste-Stephen**: How are you doing, did you solve your problem? // (I personally think it's a great thing MATLAB has evolved and so. OOP, Yay!) – r4. Jan 30 '12 at 10:29
  • 1
    The last sentence is still a relevant question. I have not figured out how to gain that functionality quite yet. Help still welcome, thanks! – St-Ste-Ste-Stephen Jan 30 '12 at 13:35
  • Some people have told me that having a method update the object like I do in the bottom is not really desirable in every case, and that the second to last usage is actually fine. I will close the question – St-Ste-Ste-Stephen Jan 30 '12 at 21:56

1 Answers1

0
Class1{index}.Class2{index} = Class1{index}.Class2{index}.Class2Method

Is the ideal way to address the stated need. It is made possible by using value classes populated by a handle class.

St-Ste-Ste-Stephen
  • 1,076
  • 10
  • 19