Default get method to return specific property
显示 更早的评论
I'm in the process of refactoring some MATLAB legacy software involving data obtained during a broad set of tests. I'm trying to create a class that contains the data of each individual channel, together with some extra info (e.g. its physical units).
Just for the sake of placing this question here, the class could look like this:
classdef Channel < handle
properties (Access = 'private')
prvValue, prvUnits;
end
properties (Dependent)
value, units;
end
methods
function this = Channel(value, units)
this.value = value;
this.units = units;
end
function set.value(this, value)
this.prvValue = value;
end
function out = get.value(this)
out = this.prvValue;
end
function set.units(this, units)
this.prvUnits = units;
end
function out = get.units(this)
out = this.prvUnits;
end
end
end
You can create an object of such a class with something like:
> ch1 = Channel([1:10], 'm');
And access to its dependent properties with:
>> ch1.value
ans =
1 2 3 4 5 6 7 8 9 10
>> ch1.units
ans =
'm'
This would nonetheless require to change every single line in the legacy code that access to the data from something like "ch1" to "ch1.value".
Now my question: is there any way to define a sort of "default get method" that returns a particular property of the class ("value", in this case)? In other words, something that behaves like this:
>> ch1
ans =
1 2 3 4 5 6 7 8 9 10
>> ch1.units
ans =
'm'
Any help will be welcome. Thanks a lot.
回答(1 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Data Type Identification 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!