create separate cells based on the (numbered) files contained in a folder
Show older comments
Hi. I would like to create separate cells based on blocks of numbers.
For example:
- Inside a folder I have 4 files numbered respectively: 378,933,934,935. How can I separate file 378 from files 933,934,935?
- Same for another folder that has within it files 147,148,149,162,163. How can I separate files 147,148,149 from files 162,163?
- Or if I have the files 156,162,163,177,178,179,180 in the folder, I should get separately: the file 156, the files 162,163 and the files 177,178,179,180.
I would then need a 'generic' code to save the blocks of numbers possibly in separate cells.
Also I have to consider that I have at least one missing file: for example in the same folder I have the numbered file with 15 and then I have the numbered file with 17 (the file with the number 16 is missing). Or the numbered file 15 and the numbered file 18 or as in the examples above (the files numbered 16 and 17 are missing).
I hope I made myself clear!
I started from knowing the numbers inside the desired folder:
filePattern_F = fullfile(pwd, '*.');
theFiles_F = dir(filePattern_F);
for k_F = 1 : length(theFiles_F)
baseFileName_F = theFiles_F(k_F).name;
fullFileName_F = fullfile(theFiles_F(k_F).folder, baseFileName_F);
end
theFiles_F(ismember({theFiles_F.name},{'.','..'})) = [];
1 Comment
dpb
on 22 Jun 2023
filePattern_F = fullfile(pwd, '*.');
d=dir(filePattern_F);
d=d(~[d.isdir]);
is a "more-MATLABy" way to return a dir() struct containing only files; naming files without an extension such as this makes it necessary to do such things; while it is possible to name folders with extensions; it usually isn't done often and so using an extension to be able to put a specific pattern in the wildcard search string will get only the files of interest.
Accepted Answer
More Answers (0)
Categories
Find more on Characters and Strings 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!