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

    function subds = partition(imds, partitionStrategy, partitionIndex)
%PARTITION Returns a partitioned portion of the ImageDatastore.
%
%   SUBDS = PARTITION(IMDS,NUMPARTITIONS,INDEX) partitions IMDS into
%   NUMPARTITIONS parts and returns the partitioned ImageDatastore, SUBDS,
%   corresponding to INDEX. An estimate for a reasonable value for the
%   NUMPARTITIONS input can be obtained by using the NUMPARTITIONS function.
%
%   SUBDS = PARTITION(IMDS,'Files',INDEX) partitions IMDS by files in the
%   Files property and returns the partition corresponding to INDEX.
%
%   SUBDS = PARTITION(IMDS,'Files',FILENAME) partitions IMDS by files and
%   returns the partition corresponding to FILENAME.
%
%   Example:
%   --------
%      folders = fullfile(matlabroot,'toolbox','matlab',{'demos','imagesci'});
%      exts = {'.jpg','.png','.tif'};
%      imds = imageDatastore(folders,'FileExtensions',exts);
%
%      % For images, numpartitions returns the number of files by default
%      n = numpartitions(imds);
%
%      % subds contains the first file from the ImageDatastore
%      subds = partition(imds,n,1);
%
%      % If not empty, read the file represented by subds
%      while hasdata(subds)
%         img = read(subds);
%      end
%
%   See also imageDatastore, numpartitions.

%   Copyright 2015 The MathWorks, Inc.

try
    if ~isempty(imds.Labels)
        % set indexes on splits which can be used to remove labels
        % after file-based partition
        setSplitsWithInfo(imds.Splitter, @iSetIndexes, 1:imds.NumFiles);
    end
    subds = partition@matlab.io.datastore.FileBasedDatastore(imds, partitionStrategy, partitionIndex);
    splits = subds.Splitter.Splits;
    if ~isempty(splits) && isfield(splits(1), 'LabelIndex')
        % Get only the Labels that are partitioned
        idxes = [splits.LabelIndex];
        % set the labels
        subds.Labels = subds.Labels(idxes);
        % set only the existing labels
        setExistingCategories(subds);
        % Get rid of the set indexes on splits of the partitioned datastore
        setSplitsWithInfo(subds.Splitter, @iRmIndexes);
    end
    if ~isempty(imds.Labels)
        % Get rid of the set indexes on splits of the original datastore
        setSplitsWithInfo(imds.Splitter, @iRmIndexes);
    end
catch e
    throw(e)
end
end

function splits = iSetIndexes(splits, indexes)
    indexes = num2cell(indexes);
    [splits.LabelIndex] = deal(indexes{:});
end
function splits = iRmIndexes(splits)
    splits = rmfield(splits, 'LabelIndex');
end