How to share and update property value across different objects.
7 次查看(过去 30 天)
显示 更早的评论
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 个评论
Steven Lord
2020-1-10
编辑:Steven Lord
2020-1-10
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.
回答(2 个)
Sean de Wolski
2020-1-10
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 个评论
Steven Lord
2020-1-10
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!