MATLAB Answers

How can I update a dependent property

1 view (last 30 days)
Amine lahemeri
Amine lahemeri on 10 Jul 2021
Answered: Cam Salzberger on 12 Jul 2021
I have a dependent property called battery_level, it is calculated based on other properties initially ( Did that with get method)
But then with time I need to increment its value (every time slot), the problem is that I need a set method to assign its value. the value that I want to update thebattery_level with is also calculated based on other properties. Is it possible to do that ?
classdef UAV < handle
properties
id {mustBeNumeric} % UAV identifier
location
mAh {mustBeNumeric} % battery milliamp hour
nominal_voltage %battery nominal voltage in Volt
battery_weight % battery weight in Kg
weight %UAV weight in Kg
state
data=0 %Harvested data in bytes
nominal_velocity=5 % Velocity in meter per seconds
velocity=0 % Instant velocity in meter per seconds
end
properties (Dependent)
battery_level %Battery capacity in Joules
total_weight % total UAV weight in Kg
end
methods
function battery_level = get.battery_level(obj)
battery_level=obj.mAh*obj.nominal_voltage*3.6;
end
...

Answers (1)

Cam Salzberger
Cam Salzberger on 12 Jul 2021
Hello Amine,
Dependent properties aren't just initially calculated based on other properties, they are always calculated using the "get" method you created. Even if you just display the object, the property will be recomputed there (assuming it's a public property that is displayed).
So if you need the value of battery_level to update after you step, and it isn't updating, make sure you are updating the properties that are used in the compuation for for battery_level.
If instead you'd rather just have the initial value computed based on other properties, but then would like to manually and directly set the value later, don't use dependent properties. Instead, set the initial value in the object constructor.
-Cam

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!