How can I link two dependent variables such that when changes are made to one, the other variable is automatically updated in MATLAB 8.1 (R2013a)?

12 views (last 30 days)
I have two kinds of data. A 'raw' data and 'derived' data. The 'derived' data is calculated from the 'raw' data. I want to link the two variables together such that changing 'raw' data in a script or interactively, automatically updates the 'derived' data. Is there a function such as LINKDATA for plots which I can make use of to achieve the above functionality?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 26 Sep 2018
Currently, MATLAB 8.1 (R2013a) does not have a function (such as LINKDATA for plots) which can monitor the changes in one variable and make changes to a dependent variable. As a workaround, one may make use of ‘classes’ and ‘events’ to achieve the desired functionality.
If you are not familiar with MATLAB classes, this paragraph might not make much sense. But the steps described in the next paragraph will still help you to make use of the attached files. Find attached a class definition file called ‘linkingClass.m’. This simple class has two properties, ‘Raw’ and ‘Derived’ and an event called ‘RawChange’. Whenever a change is made to the ‘Raw’ data, the class object makes the change by calling the method 'set.Raw'. Inside the 'set.Raw' method, the event ‘RawChange’ is triggered (using the NOTIFY function). The attached function ‘getLinkedVariables.m’ defines a listener to the event ‘RawChange’ (using ADDLISTENER) and whenever the event is triggered; it executes a callback function called ‘updateDerived’ (which is defined as a nested function within ‘getLinkedVariables.m’). The ‘updateDerived’ function in-turn calls the attached ‘raw2derived’ function which calculates the new values of ‘Derived’ data based on the new ‘Raw’ data.
Following steps describe how to use the attached files:
1) Put the script to convert ‘Raw’ to ‘Derived’ inside the body of the function ‘raw2derived.m’. The attached file uses ‘derived = 10*raw’ as an example.
2) Define ‘raw’ data. For example:
raw = [11 22 33 44 55];
3) Create an object of class ‘linkingClass’ by using the function ‘getLinkedVariables’ as follows:
my = getLinkedVariables(raw)
This will generate the following output:
Derived data updated based on changes to Raw data
my =
linkingClass with properties:
Raw: [11 22 33 44 55]
Derived: [110 220 330 440 550]
4) The variables ‘my.Raw’ and ‘my.Derived’ are now linked. Any changes made to ‘my.Raw’ will be reflected in ‘my.Derived’. For example,
my.Raw(1) = 12
This will generate the following output:
Derived data updated based on changes to Raw data
my =
linkingClass with properties:
Raw: [12 22 33 44 55]
Derived: [120 220 330 440 550]
5) Refer to your data as ‘my.Raw’ and ‘my.Derived’ everywhere else in the scripts.
Refer to the following documentation page for additional examples on how to use class events and listeners:

More Answers (0)

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Products


Release

R2013a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!