Main Content

subset

创建数据存储或 FileSet 的子集

说明

示例

subds = subset(ds,indices) 返回包含对应于 indices 的读取数据的子集。子集 subds 的类型与输入的类型相同。

  • 如果输入 ds 是数据存储,则输出 outds 是相同类型的数据存储。

  • 如果输入 dsFileSetDsFileSetBlockedFileSet 对象,则输出 subds 也分别是 FileSetDsFileSetBlockedFileSet 对象。

示例

全部折叠

创建一个图像数据存储对象,然后创建该图像数据存储的子集。

为示例文件夹中的所有图像文件创建一个图像数据存储 imds。然后,显示 imdsFiles 属性。

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

创建一个包含 imds 的前四个文件的子集数据存储 subimds,并检查 subimdsFiles 属性。

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

创建一个图像数据存储,然后创建一个仅包含指定百分比数量文件的子集数据存储,这些文件是从原始数据存储中随机选择的。

为示例文件夹中的所有图像文件创建 imageDatastore,并显示 Files 属性。此数据存储包含 8 个文件。

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

创建一组代表随机选择的文件子集的索引,该子集包含 60% 的文件。

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

     8     6     4     5     1

使用 indices 创建子集数据存储 submids,并检查其 Files 属性。

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

比较粗粒度分区和细粒度子集。

读取视频文件 xylophone.mp4 中的所有帧,并构造一个 ArrayDatastore 对象对其进行迭代。生成的对象有 141 个帧。

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

要提取一组特定的相邻帧,请创建四个粗粒度分区 arrds。提取第二个分区,它有 35 个帧。

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

使用细粒度子集在指定索引处从 arrds 中提取六个不相邻的帧。

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

输入参数

全部折叠

输入数据存储或文件集,指定为 datastoreFileSetDsFileSetBlockedFileSet 对象。

要包含在子集中的文件的索引,指定为索引向量或逻辑向量。

  • 索引向量必须包含要包含在子集 subds 中的文件的索引。

  • 逻辑向量的长度必须与输入 ds 中的文件数量相同。subset 方法将创建子集 subds,其中包含与逻辑向量中值为 true 的元素对应的文件。

indices 的元素必须是唯一的。

数据类型: double | logical

扩展功能

版本历史记录

在 R2019a 中推出