Extend vector class in matlab

I have stock price data with related dates stored in column vectors with data in MATLAB format.

I often need to do cycles or search queries on dates that I currently understand using "external accounting", for example

for i = 1:length(dates)
    thisDate = dates(i)
end

or worse

iDate = find(dates == thisDate, 1)

I usually have to do very similar operations, so I would like to create a date object that encapsulates this function for me. This is trivial to do using public properties like

dates.datenum(i)
dates.datestr(i)
dates.findOneYearEarlier(i)

and etc.

BUT

I need to have access to the vector directly in order to maintain compatibility.

Thus, direct access to my date object should result in a valid date. Example:

dates(i) == 730910              % right now
dates.datenum(i) == 730910      % how I'm able to do it

I need to work at the same time. Is there any way to do this in matlab?


, , :

  • , , , . , , ...

  • , , . , , .

  • -, , .

!

+4
1

double:

classdef myDateContainer < double

    methods
        % Constructor
        function this = myDataContainer(in)
            if nargin<1 || isempty(in)
                % default: zero
                in = 0;
            end
            this = this@double(in);
        end

        % add datestring functionality
        function str = datestring(this)
            dbl = double(this); %# cast to double to get at the value
            str = datestr(dbl); %# convert
        end
    end
end

:

date = myDateContainer(1:100);
date(3:5).datestring()
+2

Source: https://habr.com/ru/post/1542016/


All Articles