How to use Get and Set to change a property with in a class?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
I have a class to manage an array of DACs. I have a struct called DB inside the class that is used to store default settings, addresses and other needed values to interact with the hardware. For example
obj.DB.DAC1.address = 32;
obj.DB.DAC1.default = 3.0;
obj.DB.DAC1.unit = 'V';
I am trying to write the class such that when the get is executed it measures, sets the property DAC1, and returns the measured value.
function obj = get.DAC1(obj)
Value = measureDac(obj,'DAC1');
obj.DAC1 = Value;
end
measureDAC is a separate function that interacts with the hardware to measure the value using the DB struct and the string as a switch to look up the needed information in the DB.
function V=measureDAC(obj,propStr)
...
end
%To use the class DACS, for example.
dacs = DACS;
dacs.DAC1
> 2.0
This works fine by it's self and I get the behavior I was after. The issue is that I also want to use the set function to set the value of the DAC instead of measuring the value.
%This would be used as for example
dacs.DAC1 = 2.5;
>2.498
I have added to the class the set function
function obj = set.DAC1(obj,target)
Value = optimizeDac(obj,target,'DAC1');
obj.DAC1 = Value;
end
where optimizeDac is another function that uses the DB struct, the tag, and the target value to set the DAC code to the target value. Final value is measured and returned the property of the call DAC1 should be set equal to the final measured value.
This works fine by itself as well. When I combine the get and set functions in the same class I don't get the intended behavior. When the get function is called it winds up calling the set function.
function obj = get.DAC1(obj)
Value = measureDac(obj,'DAC1');
obj.DAC1 = Value; %An unexpected call to set function
end
Is there a way to avoid this behavior or work around this issue?
Thanks, Micky
0 Kommentare
Antworten (0)
Siehe auch
Kategorien
Mehr zu Data Types finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!