Python to Matlab property decorator function in class

17 views (last 30 days)
I have this piece of code in python that i want to translate to matlab. Any ideas how it could be done?
class Celsius:
def __init__(self, temperature = 0):
self._temperature = temperature
def to_fahrenheit(self):
return (self.temperature * 1.8) + 32
@property
def temperature(self):
print("Getting value")
return self._temperature
@temperature.setter
def temperature(self, value):
self._temperature = value

Answers (1)

Thibaut Jacqmin
Thibaut Jacqmin on 28 Jan 2022
classdef Celsius < handle
properties
temperature
end
methods
function obj = Celsius(temperature)
arguments
temperature double = 0
end
obj.temperature = temperature;
end
function y = to_fahrenheit(obj)
y = (obj.temperature * 1.8) + 32;
end
function y = get.temperature(obj)
disp("Getting value")
y = obj.temperature;
end
function set.temperature(obj, value)
disp("Setting value")
obj.temperature = value;
end
end
end

Community Treasure Hunt

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

Start Hunting!