0

I want to create a class that lets me load data only when a channel is requested.

I want it to work something like this:

my_file = Datafile('big_and_slow_file.dat');
ch1 = my_file.Channel_1;
ch102 = my_file.Channel_102;

In this case the file big_and_slow_file.dat might contain thousands of channels. I want to be able to use intellisense to know what channels are present in the data file but I only want it to load when it is requested.

I have started something like this

classdef Datafile < dynamicprops
    properties
        Filename
    end
    
    methods
        function obj = Datafile(filename)
       
            obj.Filename = filename;
            
            available_channels = get_available_channels(filename);
            
            % Add dynamic props for each channel
            for i = 1:length(available_channels)
                channel_i = available_channels{i};
                
                p=obj.addprop(plotvar)
                p.GetObservable = true;
                addlistener(obj, plotvar, 'PreGet', @obj.property1Getter);
            end
        end
    end
    
    methods (Access = private)
        function value = property1Getter(obj, p, ~)
           
            channel_id = p.Name;
            
            if isempty(obj.(channel_id))
                obj.(channel_id) = load_data(obj.Filename,channel_id);
            end
        end
    end
end

The problem I experience is that the getter is called when I instanciate my object. This makes it so that it tries to load all channels when I create my object. I would like it to only load data when explicitly called. Is this possible?

0 Answers0