Default get method that returns specific property - MATLAB

可紊 提交于 2020-07-09 06:50:53

问题


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:


No, ch1 by itself is the object. You can have MATLAB output "ans = 1 2 3 ..." if you type ch1 by itself (by overloading the display method), but this will not actually assign that value to ans. In particular, you cannot change the result of foo = ch1, foo will always be the object itself.

On the other hand, you can overload the double method, such that foo = double(ch1) yields the same as foo = ch1.value.


Do note that

I'm in the process of refactoring some MATLAB legacy software [...]

sort of automatically leads to

This would nonetheless require to change every single line in the legacy code [...]

Refactoring code involves a lot of rewriting. If you don't want to change so much code, make the refactoring less invasive. Don't change a plain array to a complex object.




回答2:


I have in the meantime found something that seems -so far- to do exactly what I was aiming at: make the new class inherit from MATLAB's standard double class:

classdef ChannelDouble < double
    properties
        units
    end

    methods
        function this = ChannelDouble(data, units)            
            this = this@double(data);
            this.units = units;            
        end
    end  
end

This class returns:

 >> ch1 = ChannelDouble([1:10], 'm')

ch1 = 

  1×10 ChannelDouble array with properties:

    units: 'm'

  double data:
     1     2     3     4     5     6     7     8     9    10

>> ch1.^2  % <--- Standard notation returns the data just like a double array would do

ans =

     1     4     9    16    25    36    49    64    81   100

>> ch1.units

ans =

    'm'

If anybody can think of a better solution, please let me know. Otherwise I will leave this reply here in case it can help anybody with a similar problem.



来源:https://stackoverflow.com/questions/60968076/default-get-method-that-returns-specific-property-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!