MATLAB Answers

How to share and update property value across different objects.

3 views (last 30 days)
Elvicharde
Elvicharde on 10 Jan 2020
Answered: Elvicharde on 20 Jan 2020
Hello, I am trying to share the value an object's property with another object of a different class. The idea is that a sensor is measuring that value, and this will require the sensor to get an update on changes to that value (and set a property within it to that value). Here are the samples from the two classes (they are excerpts from the whole code):
% Here is the first class (Excerpt)
classdef virtual_Patient < handle
% This is a virtual patient object.
% Here are the two properties that i want the sensor to read
properties (SetObservable, AbortSet)
% Parameters
plasma_InsulinConcentration % I(t) [mU/L]
interstitial_GlucoseConcentration
end
% Here is the second class (Excerpt)
classdef cgmObj< handle
% This object represents the continuous glucose monitor. The aim is to
% take real-time values of the interstitial glucose conentration and the
% plasma insulin cocncentration. This accounts for the parametric
% delays in reporting by adding a noise signal to the model.
properties
interstitial_BG
Vn
err_Factor
Error
end
properties (Dependent)
CGM_Signal
end
I have tried using the 'post set' event method to get the values from the first class, but I can't seem to update the second object with the value. Here is what I have tried:
classdef cgmObj< handle
methods (Static)
function updateBG (src,event)
switch src.Name
case 'interstitial_GlucoseConcentration'
val = event.AffectedObject.interstitial_GlucoseConcentration;
cgmObj.set_interstitial_BG(val);
disp (['Updated value ',num2str(val(end))])
case 'plasma_InsulinConcentration'
disp('Insulin levels changed too')
end
end
end
methods
function CGM = cgmObj(obj)
if nargin > 0
addlistener(obj,'interstitial_GlucoseConcentration',...
'PostSet', @cgmObj.updateBG);
addlistener(obj,'plasma_InsulinConcentration',...
'PostSet', @cgmObj.updateBG);
s = rng(); % keeping state
CGM.Vn = rand;
CGM.err_Factor = CGM.Vn(1,1);
rng(s);
CGM.Error = 0;
end
end
I keep getting this error:
Patient.interstitial_GlucoseConcentration(6) = 8
Warning: Error occurred while executing the listener callback for
the virtual_Patient class interstitial_GlucoseConcentration
property PostSet event:
The class cgmObj has no Constant property or Static method named
'set_interstitial_BG'.
Error in cgmObj.updateEvents (line 29)
cgmObj.set_interstitial_BG;
Warning: Error occurred while executing the listener callback for
the virtual_Patient class interstitial_GlucoseConcentration
property PostSet event:
The class cgmObj has no Constant property or Static method named
'set_interstitial_BG'.
Error in cgmObj.updateEvents (line 29)
cgmObj.set_interstitial_BG;
Warning: Error occurred while executing the listener callback for
the virtual_Patient class interstitial_GlucoseConcentration
property PostSet event:
The class cgmObj has no Constant property or Static method named
'set_interstitial_BG'.
Error in cgmObj.updateEvents (line 29)
cgmObj.set_interstitial_BG;
Warning: Error occurred while executing the listener callback for
the virtual_Patient class interstitial_GlucoseConcentration
property PostSet event:
The class cgmObj has no Constant property or Static method named
'set_interstitial_BG'.
Error in cgmObj.updateEvents (line 29)
cgmObj.set_interstitial_BG;
Warning: Error occurred while executing the listener callback for
the virtual_Patient class interstitial_GlucoseConcentration
property PostSet event:
The class cgmObj has no Constant property or Static method named
'set_interstitial_BG'.
Error in cgmObj.updateEvents (line 29)
cgmObj.set_interstitial_BG;
Warning: Error occurred while executing the listener callback for
the virtual_Patient class interstitial_GlucoseConcentration
property PostSet event:
The class cgmObj has no Constant property or Static method named
'set_interstitial_BG'.
Error in cgmObj.updateEvents (line 29)
cgmObj.set_interstitial_BG;
Patient =
virtual_Patient with properties:
plasma_InsulinConcentration: 5.8000
interstitial_GlucoseConcentration: [4.4000 1 5 5.2000 13 8]
I have also tried to just pass the first class object to the second, but it does not update the values unless I recreate the object all over.
I am new to OOP with matlab, and this (oop) is the most intuitive way for me to implement this project. Any help with this, or a better approach to achieve this will be a tremendous help.

  2 Comments

Steven Lord
Steven Lord on 10 Jan 2020
It's not clear to me how your virtual_Patient and cgmObj objects are related. Does one of them contain an instance of the other as a property? Are they both properties of a third object? Are they independent but communicating through events?
Perhaps if you take a step back from the code and explain the system your objects are trying to represent in words (and maybe a picture or two), that may help us help you determine the right design and implementation for those objects.
Elvicharde
Elvicharde on 10 Jan 2020
I see my error at clarification. They are meant to be independent objects communicating through events and listeners (or any other way I can share property values between two matlab objects). The idea is this: the virtual patient represents a test_patient (virtual person) with measurable properties e.g. heart rate, and the cgmObj is a sensor that detects this property. To run, I will manually create an instance of both classes via their constructors. The sensor's constructor accepts the virtual patient object as an argument, and it can access its property at that instantiation (this is how I tried to share the property earlier. It worked, but I had to keep re-creating the sensor object to update the property value—which is not ideal for my program). I hope this clarifies the task I have?

Sign in to comment.

Answers (2)

Sean de Wolski
Sean de Wolski on 10 Jan 2020
Put a breakpoint on this line:
cgmObj.set_interstitial_BG(val);
And see what cgmObj is and if it has a set_interstitial_BG(val); method which it looks like it does not.

  2 Comments

Elvicharde
Elvicharde on 10 Jan 2020
I do have this method in the class implementation. I have tried debugging, but I think my error is in my approach to set a value outside the 'events workspace'. Once the "Update events " method is called, I noticed that the workspace only has the events and source values listed. Hence, if i set a value to "val" within that workspace, it gets destroyed upon completion of the method action.
classdef cgmObj< handle
% This object represents the continuous glucose monitor. The aim is to
% take real-time values of the interstitial glucose conentration and the
% plasma insulin cocncentration. This accounts for the parametric
% delays in reporting by adding a noise signal to the model.
properties
interstitial_BG
Vn
err_Factor
Error
end
properties (Dependent)
CGM_Signal
end
properties (Constant)
zeta = -5.471;
lambda = 15.96;
gamma = -0.5444;
delta = 1.6898;
end
methods (Static)
function updateEvents(src,event)
switch src.Name
case 'interstitial_GlucoseConcentration'
val = event.AffectedObject.interstitial_GlucoseConcentration;
cgmObj.set_interstitial_BG;
disp (['Updated values ',num2str(val(end))])
case 'plasma_InsulinConcentration'
disp('Insulin levels changed too')
end
end
end
methods
function CGM = cgmObj(obj)
if nargin > 0
addlistener(obj,'interstitial_GlucoseConcentration',...
'PostSet', @cgmObj.updateEvents);
addlistener(obj,'plasma_InsulinConcentration',...
'PostSet', @cgmObj.updateEvents);
s = rng(); % keeping state
CGM.Vn = rand;
CGM.err_Factor = CGM.Vn(1,1);
rng(s);
CGM.Error = 0;
end
end
function set_interstitial_BG(CGM,value)
CGM.interstitial_BG = value;
end
function out = get.CGM_Signal(CGM)
diff = length(CGM.interstitial_BG) - length(CGM.Vn);
Z = CGM.zeta;
L = CGM.lambda;
G = CGM.gamma;
D = CGM.delta;
if diff > 0
updateVN = randn(1,diff); % standard normal distribution N~(0,1)
CGM.Vn = [CGM.Vn,updateVN];
for i = 2:length(CGM.Vn)
updateErr = 0.7 * (CGM.err_Factor(:,i-1)+CGM.Vn(:,i));
CGM.err_Factor = [CGM.err_Factor,updateErr];
end
end
CGM.Error = Z + (L*sinh((CGM.err_Factor - G)/D));
out = CGM.interstitial_BG + CGM.Error;
end
end
end
Here is the full code for the sensor object. Thanks for your inputs so far.
Steven Lord
Steven Lord on 10 Jan 2020
Elvicharde, FYI flagging a comment is intended to be used to inform people with sufficient permissions to evaluate that comment to determine if it's off-topic, spam, etc. and should be deleted. That's not the case here, since you had flagged your own comment to indicate that it was a clarification. I've removed the flag.

Sign in to comment.


Elvicharde
Elvicharde on 20 Jan 2020
Thanks a lot guys for your contributions; however, I have figured it out on my own.
I decided to pass the object I need monitored (which is an object of a handle-type class) to a variable during the instantiation of my sensor. This way, I can just call the that reference handle and pass its value to my sensor object.
classdef cgmObj< handle
% This object represents the continuous glucose monitor. The aim is to
% take real-time values of the plasma glucose conentration and the
% plasma insulin cocncentration. This accounts for the parametric
% delays in reporting by adding a noise signal to the model.
properties
patient_Type
Error
end
properties (Hidden)
monitoredObject % This keeps the handle for the Patient.
Vn
err_Factor
end
properties (Dependent)
interstitial_BG;
CGM_Signal
end
properties (Constant, Hidden)
zeta = -5.471;
lambda = 15.96;
gamma = -0.5444;
delta = 1.6898;
end
methods (Static)
function CGM = cgmObj(obj)
CGM.monitoredObject = obj; % Storing the object handle as a property of the sensor object
CGM.patient_Type = obj.diabetes_Variant;
s = rng(); % keeping state
CGM.Vn = rand;
CGM.err_Factor = CGM.Vn(1,1);
rng(s);
CGM.Error = 0;
end
end
methods
function out = get.interstitial_BG(CGM)
out = CGM.monitoredObject.interstitial_GlucoseConcentration; % This is the parameter I need to monitor (from the monitored object)
end

  0 Comments

Sign in to comment.

Sign in to answer this question.

Products


Release

R2018a