Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8293499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:50:51+00:00 2026-06-08T13:50:51+00:00

In Matlab class it seems to be syntactically correct to declare property that is

  • 0

In Matlab class it seems to be syntactically correct to declare property that is Dependent (computed not stored) and Observable in the same time. Consider code

properties (Access = private)
    instanceOfAnotherClass
end
properties (SetAccess = private, Dependent, SetObservable)
    propertyTwo
end
methods
    function val = get.propertyTwo(this)
        val = this.instanceOfAnotherClass.propertyOne;   
    end
end

Does this work as expected? That is, if the property propertyOne of an object stored in instanceOfAnotherClass is changed is there property change event triggered by propertyTwo? Note that propertyOne is not Observable.

Edit:
It does not work (as I expected). ‘PostSet’ event is not triggered. So how do I deal with this kind of situation? Is there a better solution then to create propertyTwo as a non Dependent and set it to the same value as ‘propertyOne’ every time ‘propertyOne’ changes?

Edit2:
In reaction to Amro’s edit of his answer I will explain situation more complex.
Consider this 2 classes:

 classdef AClass < handle
     properties
         a
     end
 end
 classdef BClass < handle
     properties (Access = private)
         aClassInst
     end
     properties (Dependent, SetObservable, SetAccess = private)
         b
     end
     methods
         function this = BClass(aClass)
             this.aClassInst = aClass;
         end
         function val = get.b(this)
             val = this.aClassInst.a;
         end
     end
 end

The class that uses all this code should not get access to AClass. It interacts only with instance of BClass and wants to listen to changes of property b. however if I make property a of AClass observable that would not solve my problem, would it? The ‘PostSet’ events are not going to propagate to property b, are they?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T13:50:52+00:00Added an answer on June 8, 2026 at 1:50 pm

    It might be syntactically correct, but the listener callback will never execute. Example:

    classdef MyClass < handle
        properties (Access = public)
            a
        end
        properties (SetAccess = private, Dependent, SetObservable)
            b
        end
        methods
            function val = get.b(this)
                val = this.a;
            end
        end
    end
    

    Now try:

    c = MyClass();
    lh = addlistener(c, 'b', 'PostSet',@(o,e)disp(e.EventName));
    c.a = 1;
    disp(c.b)
    

    As you can see the ‘PostSet’ callback is never executed.


    EDIT

    The way I see it, SetObservable should really be set on a not b. Its because b is read-only and can only change if a changes. Now the PostSet event would notify us that both properties have changed.

    Use the same example I used above, simply move SetObservable from b to a. Of course now you listen to the event as:

    lh = addlistener(c, 'a', 'PostSet',@(o,e)disp(e.EventName));
    

    EDIT#2

    Sorry I didn’t pay attention to the fact that you have composition (BClass has an instance of AClass as private property).

    Consider this possible solution:

    AClass.m

    classdef AClass < handle
        properties (SetObservable)
            a                        %# observable property
        end
    end
    

    BClass.m

    classdef BClass < handle
        properties (Access = private)
            aClassInst               %# instance of AClass
            lh                       %# event listener on aClassInst.a
        end
        properties (Dependent, SetAccess = private)
            b                        %# dependent property, read-only
        end
        events (ListenAccess = public, NotifyAccess = private)
            bPostSet                 %# custom event raised on b PostSet
        end
        methods
            function this = BClass(aClass)
                %# store AClass instance handle
                this.aClassInst = aClass;
                %# listen on PostSet event for property a of AClass instance
                this.lh = addlistener(this.aClassInst, 'a',  ...
                    'PostSet', @this.aPostSet_EventHandler);
            end
            function val = get.b(this)
                val = this.aClassInst.a;
            end
        end
        methods (Access = private)
            function aPostSet_EventHandler(this, src, evt)
                %# raise bPostSet event, notifying all registered listeners
                notify(this, 'bPostSet')
            end
        end
    end
    

    Basically we set property a of AClass as observable.

    Next inside the constructor of BClass, we register a listener for the AClass instance passed to listen on property a changes. In the callback we notify listeners of this object that b has changed as well

    Since we can’t really raise a PostSet manually, I created a custom event bPostSet which we raise in the previous callback function. You can always customize the event data passed, refer to the documentation to see how.

    Here is a test case:

    %# create the objects
    a = AClass();
    b = BClass(a);
    
    %# change property a. We will not recieve any notification
    disp('a.a = 1')
    a.a = 1;
    
    %# now lets listen for the 'bChanged' event on b
    lh = addlistener(b, 'bPostSet',@(o,e) disp('-- changed'));
    
    %# try to change the property a again. We shall see notification
    disp('a.a = 2')
    a.a = 2;
    
    %# remove event handler
    delete(lh)
    
    %# no more notifications
    disp('a.a = 3')
    a.a = 3;
    

    The output was:

    a.a = 1
    a.a = 2
    -- changed
    a.a = 3
    

    Notice how we only interact with the BClass instance when we register our listener. Of course since all classes derive from handle class, the instance a and the private property aClassInst both refer to the same object. So any changes to a.a are immediately reflected on b.aClassInst.a, this causes the internal aPostSet_EventHandler to execute, which in turn notify all registered listeners to our custom event.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why is that in every class in MATLAB I must use this? I think
I have a class called forest and a property called fixedPositions that stores 100
Is it possible to overload a function in a Matlab class that you've created?
I'm wondering why matlab does not display access types of class methods and attributes
I have a class with a few properties that are dependent, but that I'd
I'm trying to implement a class with a property that can be possibly provided
I declare a class in matlab and here is the constructor and a function.
I have a MATLAB class that contains a method that employs a persistent variable.
In a MATLAB class I am writing, if the constructor is given 0 arguments,
Question about subclassing in matlab, under the new class system. I've got class A

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.