Python to Matlab property decorator function in class

8 ビュー (過去 30 日間)
Nefeli Prompona
Nefeli Prompona 2019 年 4 月 9 日
回答済み: Thibaut Jacqmin 2022 年 1 月 28 日
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

回答 (1 件)

Thibaut Jacqmin
Thibaut Jacqmin 2022 年 1 月 28 日
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

カテゴリ

Help Center および File ExchangeCall Python from MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by