Deleting certain elements from an array without looping
Mostra commenti meno recenti
Hi folks,
I have a cell array containing all the file names in a subfolder. I want to remove the filenames that are not 21 characters in length.
I have tried:
fileNames(length(fileNames(:)) ~= 21) = [];
but this only deletes one entry each time I run it, and not the correct ones eather!
May I please ask where I've gone wrong and how to fix it?
full code:
subFolders = dir(sourcePath);
subFolders = subFolders([subFolders(:).isdir]);
subFolders = subFolders(~ismember({subFolders(:).name},{'.','..'}));
fileNum = numel(subFolders);
folderDir = fullfile(subFolders(i).folder, subFolders(i).name);
folderDir = dir(folderDir);
folderDir = folderDir(~ismember({folderDir(:).name},{'.','..'}));
fileNames = {folderDir.name};
Thanks!
3 Commenti
Rik
il 8 Nov 2021
What data type is fileNames?
Chunru
il 8 Nov 2021
Can you provide a small part of fileNames so we know what it looks like?
Teshan Rezel
il 8 Nov 2021
Risposta accettata
Più risposte (1)
fileNames = {'ThisIsADummyFile', '012345678901234567891'}
idx = cellfun(@(x) length(x)~=21, fileNames)
fileNames(idx) =[]
Categorie
Scopri di più su Performance and Memory in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!