Main Content

subset

Create subset of datastore or file-set

Description

example

subds = subset(ds,indices)returns a subset containing files corresponding toindices. The subsetsubdsis of the same type as the input.

  • if the inputdsis a datastore, then the outputoutdsis a datastore of the same type.

  • if the inputdsis aDsFileSet,FileSet, orBlockedFileSetobject, then the outputsubdsis also, respectively, aDsFileSet,FileSet, orBlockedFileSetobject.

Examples

collapse all

Make an image datastore object and then create a subset of that image datastore.

Create an image datastoreimdsfor all the image files in a sample folder. Then, display theFilesproperty ofimds.

folders = fullfile(matlabroot,'toolbox','matlab',{'demos','imagesci'}); exts = {'.jpg','.png','.tif'}; imds = imageDatastore(folders,'LabelSource','foldernames','FileExtensions',exts); imds.Files
ans = 8×1 cell array {'...\matlab\toolbox\matlab\demos\cloudCombined.jpg'} {'...\matlab\toolbox\matlab\demos\example.tif' } {'...\matlab\toolbox\matlab\demos\landOcean.jpg' } {'...\matlab\toolbox\matlab\demos\ngc6543a.jpg' } {'...\matlab\toolbox\matlab\demos\street1.jpg' } {'...\matlab\toolbox\matlab\demos\street2.jpg' } {'...\matlab\toolbox\matlab\imagesci\corn.tif' } {'...\matlab\toolbox\matlab\imagesci\peppers.png' }

Create a subset datastoresubimds包含第一个四files ofimdsand examine theFilesproperty ofsubimds.

indices = 1:4; subimds = subset(imds,indices); subimds.Files
ans = 4×1 cell array {'...\matlab\toolbox\matlab\demos\cloudCombined.jpg'} {'...\matlab\toolbox\matlab\demos\example.tif' } {'...\matlab\toolbox\matlab\demos\landOcean.jpg' } {'...\matlab\toolbox\matlab\demos\ngc6543a.jpg' }

Make an image datastore, and then create subset datastore containing only a specified percentage of files, randomly selected from the original datastore.

CreateimageDatastorefor all the image files in a sample folder and display theFilesproperty. This datastore contains 8 files.

folders = fullfile(matlabroot,'toolbox','matlab',{'demos','imagesci'}); exts = {'.jpg','.png','.tif'}; imds = imageDatastore(folders,'LabelSource','foldernames','FileExtensions',exts); imds.Files
ans = 8×1 cell array {'...\matlab\toolbox\matlab\demos\cloudCombined.jpg'} {'...\matlab\toolbox\matlab\demos\example.tif' } {'...\matlab\toolbox\matlab\demos\landOcean.jpg' } {'...\matlab\toolbox\matlab\demos\ngc6543a.jpg' } {'...\matlab\toolbox\matlab\demos\street1.jpg' } {'...\matlab\toolbox\matlab\demos\street2.jpg' } {'...\matlab\toolbox\matlab\imagesci\corn.tif' } {'...\matlab\toolbox\matlab\imagesci\peppers.png' }

Create a set of indices that represents randomly selected subset containing60%of the files.

nFiles = length(imds.Files); RandIndices = randperm(nFiles); nSixtyPercent = round(0.6*nFiles); indices = RandIndices(1:nSixtyPercent)
indices = 8 6 4 5 1

Create a subset datastoresubmidsusingindicesand examine itsFilesproperty.

subimds = subset(imds,indices); subimds.Files
ans = 5×1 cell array {'...\matlab\toolbox\matlab\imagesci\peppers.png' } {'...\matlab\toolbox\matlab\demos\street2.jpg' } {'...\matlab\toolbox\matlab\demos\ngc6543a.jpg' } {'...\matlab\toolbox\matlab\demos\street1.jpg' } {'...\matlab\toolbox\matlab\demos\cloudCombined.jpg'}

Input Arguments

collapse all

Input datastore or file-set, specified asImageDatastore,DsFileSet,FileSet,BlockedFileSetobject.

Indices of files to include in subset, specified as a vector of indices or a logical vector.

  • The vector of indices must contain the indices of files to include in the subsetsubds.

  • The logical vector must be of the same length as the number of files in the inputds. Thesubsetmethod creates a subsetsubdscontaining files corresponding to the elements in the logical vector that have a value oftrue.

Elements ofindicesmust be unique.

Data Types:double|logical

Extended Capabilities

Version History

Introduced in R2019a