www.gusucode.com > datastoreio工具箱 matlab源码程序 > datastoreio/+matlab/+io/+datastore/@FileDatastore/FileDatastore.m

    classdef (Sealed) FileDatastore < ...
                  matlab.io.datastore.CustomReadDatastore & ...
                  matlab.io.datastore.internal.ScalarBase & ...
                  matlab.mixin.CustomDisplay
%FILEDATASTORE Datastore for a collection of files with custom data format.
%   FDS = fileDatastore(LOCATION,'ReadFcn',@MYCUSTOMREADER) creates a
%   FileDatastore if a file or a collection of files are present in LOCATION.
%   LOCATION has the following properties:
%      - Can be a filename or a folder name
%      - Can be a cell array of multiple file or folder names
%      - Can contain a relative path (HDFS requires a full path)
%      - Can contain a wildcard (*) character.
%   'ReadFcn',@MYCUSTOMREADER Name-Value pair specifies the user-defined
%   function to read files. The value of 'ReadFcn' must be a function handle
%   with a signature similar to the following:
%      function data = MYCUSTOMREADER(filename)
%      ..
%      end
%
%   FDS = fileDatastore(__,'IncludeSubfolders',TF) specifies the logical
%   true or false to indicate whether the files in each folder and its
%   subfolders are included recursively or not.
%
%   FDS = fileDatastore(__,'FileExtensions',EXTENSIONS) specifies the
%   extensions of files to be included. Values for EXTENSIONS can be:
%      - A character vector, such as '.jpg' or '.png' (empty quotes '' are
%        allowed for files without extensions)
%      - A cell array of character vectors, such as {'.jpg', '.mat'}
%
%   FileDatastore Properties:
%
%      Files           - Cell array of file names
%      ReadFcn         - Function handle used to read files
%
%   FileDatastore Methods:
%
%      hasdata       - Returns true if there is more data in the datastore
%      read          - Reads the next consecutive file
%      reset         - Resets the datastore to the start of the data
%      preview       - Reads the first file from the datastore for preview
%      readall       - Reads all of the files from the datastore
%      partition     - Returns a new datastore that represents a single
%                      partitioned portion of the original datastore
%      numpartitions - Returns an estimate for a reasonable number of
%                      partitions according to the total data size to use
%                      with the partition function
%
%   Example:
%   --------
%      folder = fullfile(matlabroot,'toolbox','matlab','demos');
%      fds = fileDatastore(folder,'ReadFcn',@load,'FileExtensions','.mat');
%
%      data1 = read(fds);                   % Read the first MAT-file
%      data2 = read(fds);                   % Read the next MAT-file
%      readall(fds)                         % Read all of the MAT-files
%      dataArr = cell(numel(fds.Files),1);
%      i = 1;
%      reset(fds);                          % Reset to the beginning of data
%      while hasdata(fds)                   % Read files using a while-loop
%          dataArr{i} = read(fds);
%          i = i + 1;
%      end
%
%   See also datastore, mapreduce, load, fileDatastore.

%   Copyright 2015 The MathWorks, Inc.

    properties (Dependent)
        %Files -
        % A cell array of file names.
        Files;
    end

    properties (Access = private)
        % deployement needs a way to get files before resolving them
        UnResolvedFiles;
    end

    properties (Constant, Access = private)
        WHOLE_FILE_CUSTOM_READ_SPLITTER_NAME = 'matlab.io.datastore.splitter.WholeFileCustomReadSplitter';
        CONVENIENCE_CONSTRUCTOR_FCN_NAME = 'fileDatastore';
    end
    % Constructor
    methods
        % FileDataStore can be constructed with files argument, optionally
        % with ReadFcn, IncludeSubfolders, FileExtensions Name-Value pairs.
        function fds = FileDatastore(files, varargin)
            try
                import matlab.io.datastore.FileDatastore;
                nv = iParseNameValues(varargin{:});
                initDatastore(fds, files, nv);
                fds.UnResolvedFiles = files;
            catch e
                throwAsCaller(e);
            end
        end
    end

    % Set and Get methods for properties
    methods
        % Set method for Files
        function set.Files(fds, files)
            try
                setNewFilesAndFileSizes(fds, files);
            catch e
                throw(e)
            end
        end
        % Get Files
        function files = get.Files(fds)
            files = fds.Splitter.Files;
        end
    end

    methods (Access = private)

        function initDatastore(fds, files, nv)
            import matlab.io.datastore.FileDatastore;
            import matlab.io.datastore.internal.validators.validateCustomReadFcn;

            readFcn = nv.ReadFcn;
            if ismember('ReadFcn', nv.UsingDefaults) && ...
                    isnumeric(readFcn) && readFcn == -1
                error(message('MATLAB:datastoreio:filedatastore:readFcnNotProvided'));
            end
            validateCustomReadFcn(readFcn, true, FileDatastore.CONVENIENCE_CONSTRUCTOR_FCN_NAME);
            [~, files, fileSizes] = FileDatastore.supportsLocationHelper(files, nv);
            fds.SplitterName = FileDatastore.WHOLE_FILE_CUSTOM_READ_SPLITTER_NAME;
            % Files are resolved @supportsLocationHelper
            resolved = true;
            initFromReadFcn(fds, readFcn, files, fileSizes, nv.IncludeSubfolders);
        end
    end

    methods (Access = protected)

        function validateReadFcn(~, readFcn)
            import matlab.io.datastore.FileDatastore;
            import matlab.io.datastore.internal.validators.validateCustomReadFcn;
            validateCustomReadFcn(readFcn, false, FileDatastore.CONVENIENCE_CONSTRUCTOR_FCN_NAME);
        end

        function displayScalarObject(fds)
            % header
            disp(getHeader(fds));
            group = getPropertyGroups(fds);
            detailsStr = evalc('details(fds)');
            nsplits = strsplit(detailsStr, '\n');
            filesStr = nsplits(~cellfun(@isempty, strfind(nsplits, 'Files: ')));
            % Find the indent spaces from details
            nFilesIndent = strfind(filesStr{1}, 'Files: ') - 1;
            if nFilesIndent > 0
                % File Properties
                filesIndent = [sprintf(repmat(' ',1,nFilesIndent)) 'Files: '];
                nlspacing = sprintf(repmat(' ',1,numel(filesIndent)));
                if isempty(fds.Files)
                    nlspacing = '';
                end
                import matlab.io.datastore.internal.cellArrayDisp;
                filesStrDisp = cellArrayDisp(fds.Files, true, nlspacing);
                disp([filesIndent filesStrDisp]);
                % Remove Files property from the group, since custom
                % display is used for Files.
                group.PropertyList = rmfield(group.PropertyList, 'Files');
            end
            matlab.mixin.CustomDisplay.displayPropertyGroups(fds, group);
            disp(getFooter(fds));
        end
    end

    methods (Hidden)
        function files = getUnresolvedFiles(fds)
            files = fds.UnResolvedFiles;
        end
    end

    methods (Static, Hidden)

        function tf = supportsLocation(~, ~)
            % This function is responsible for determining whether a given
            % location is supported by FileDatastore. For FileDatastore
            % 'Type' Name-Value pair must be provided for datastore function.

            tf = false;
        end

        function varargout = supportsLocationHelper(loc, nvStruct)
            % This function is responsible for determining whether a given
            % location is supported by FileDatastore. It also returns a
            % resolved filelist.
            defaultExtensions = {};
            % validate file extensions, include subfolders is validated in
            % pathlookup
            import matlab.io.datastore.internal.validators.validateFileExtensions;
            import matlab.io.datastore.FileBasedDatastore;

            isDefaultExts = validateFileExtensions(nvStruct.FileExtensions, nvStruct.UsingDefaults);
            [varargout{1:nargout}] = FileBasedDatastore.supportsLocation(loc, nvStruct, defaultExtensions, ~isDefaultExts);
        end

        function outds = loadobj(ds)
            if ds.Splitter.NumSplits ~= 0
                % create a split reader that points to the
                % first split index.
                if ds.SplitIdx == 0
                    ds.SplitIdx = 1;
                end
                ds.SplitReader = ds.Splitter.createReader(ds.SplitIdx);
            end
            outds = loadobj@matlab.io.datastore.FileBasedDatastore(ds);
        end
    end
end

function parsedStruct = iParseNameValues(varargin)
    persistent inpP;
    if isempty(inpP)
        import matlab.io.datastore.FileDatastore;
        inpP = inputParser;
        addParameter(inpP, 'ReadFcn', -1);
        addParameter(inpP, 'IncludeSubfolders', false);
        addParameter(inpP, 'FileExtensions', -1);
        inpP.FunctionName = FileDatastore.CONVENIENCE_CONSTRUCTOR_FCN_NAME;
    end
    parse(inpP, varargin{:});
    parsedStruct = inpP.Results;
    parsedStruct.UsingDefaults = inpP.UsingDefaults;
end