Grouping data from grp2idx output
Show older comments
I have an nx3 cell array, where n>1000, containing file info from a directory and its subfolders.
exampleFileInfo = {'/path/to/subfolder', 'filename', 'ext'; '/path/to/subfolder', 'filename', 'ext'};
I would like to group this data so that groupData{i} returns a cell array of the files in the ith subfolder.
[ind, dirStr] = grp2idx returns a nx1 array of indecies where exampleFileInfo{i, 1} is in the directory represented by the value of ind{i}, and that directory path is returned by dirStr(ind(i)).
What is the best way to produce the groupData array where groupData{i} contains all the filenames in directory dirStr(ind(i)).
My current code works but uses a for loop, is there a better way of doing this?
currDir = uigetdir;
dirLst = rdir([currDir, '\**\*.ext']);
fileNames = strrep({dirLst.name}', currDir, '');
filePathInfo = cell(numel(fileNames), 3);
[filePathInfo(:, 1), filePathInfo(:, 2), filePathInfo(:, 3)] = cellfun(@(f) fileparts(f), fileNames, 'UniformOutput', false);
[groupID, groupString] = grp2idx(filePathInfo(:, 1));
fileInfo = cell(max(groupID), 1);
ids = min(groupID):max(groupID);
for i = min(groupID):max(groupID)
indecies{i} = find(groupID==i);
groupFiles{i} = filePathInfo(indecies{i}, 2);
end
Thanks,
Answers (1)
Sean de Wolski
on 27 Nov 2012
If what you have works, I would use it!
Just make sure to preallocate the cells befor ethe for-loop
groupFiles = cell(max(groupID),1);
indecies = groupFiles;
for ii = ...
Categories
Find more on Search Path in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!