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

    function dsOut = shuffle(ds)
%shuffle Shuffle the files of a copy of the ImageDatastore.
%   DSOUT = SHUFFLE(DS) creates a deep copy of the input ImageDatastore and
%   shuffles the files using randperm, resulting in the datastore DSOUT.
%
%   Example:
%   --------
%      folders = fullfile(matlabroot,'toolbox','matlab',{'demos','imagesci'});
%      exts = {'.jpg','.png','.tif'};
%      imds = imageDatastore(folders,'LabelSource','foldernames','FileExtensions',exts)
%      shuffledDs = shuffle(imds)
%
%   See also imageDatastore, splitEachLabel, countEachLabel, hasdata,
%   readimage, readall, preview, reset.

%   Copyright 2015 The MathWorks, Inc.
try
    dsOut = copy(ds);
    rndIdxes = randperm(dsOut.NumFiles);
    % Signature for initFromReadFcn:
    %    initFromReadFcn(ds, readFcn, files, resolved, includeSubfolders)
    %
    %  - resolved = false. Files are already resolved so we don't lookup
    %    the path and verify the existence of files
    %  - includeSubfolders = false. Files are resolved and none of them
    %    are folders.
    initFromReadFcn(dsOut, dsOut.ReadFcn, dsOut.Files(rndIdxes), true, false);
    if ~isempty(dsOut.Labels)
        dsOut.Labels = dsOut.Labels(rndIdxes);
    end
catch e
    throw(e)
end
end