subset - Create subset of datastore or FileSet - MATLAB (original) (raw)

Create subset of datastore or FileSet

Syntax

Description

subds = subset([ds](#mw%5Fdb26d859-83b0-4d4b-a2ab-f663a43ec943),[indices](#mw%5Fc4088c28-75db-4a09-bee5-af9532d66ed0)) returns a subset containing reads corresponding to indices. The subsetsubds is of the same type as the input.

example

Examples

collapse all

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

Create an image datastore imds for all the image files in a sample folder. Then, display the Files property 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 datastore subimds that contains the first four files of imds and examine the Files property 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.

Create imageDatastore for all the image files in a sample folder and display the Files property. 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)

Create a subset datastore submids usingindices and examine its Files property.

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'}

Compare a coarse-grained partition with a fine-grained subset.

Read all the frames in the video file xylophone.mp4 and construct an ArrayDatastore object to iterate over it. The resulting object has 141 frames.

v = VideoReader("xylophone.mp4"); allFrames = read(v); arrds = arrayDatastore(allFrames,IterationDimension=4,OutputType="cell",ReadSize=4);

To extract a specific set of adjacent frames, create four coarse-grained partitions of arrds. Extract the second partition, which has 35 frames.

partds = partition(arrds,4,2); imshow(imtile(partds.readall()))

Figure contains an axes object. The hidden axes object contains an object of type image.

Extract six nonadjacent frames from arrds at specified indices using a fine-grained subset.

subds = subset(arrds,[67 79 82 69 89 33]); imshow(imtile(subds.readall()))

Figure contains an axes object. The hidden axes object contains an object of type image.

Input Arguments

collapse all

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

Elements of indices must be unique.

Data Types: double | logical

Extended Capabilities

expand all

Usage notes and limitations:

For more information, see Run MATLAB Functions in Thread-Based Environment.

Version History

Introduced in R2019a