Filter löschen
Filter löschen

create separate cells based on the (numbered) files contained in a folder

2 Ansichten (letzte 30 Tage)
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 Kommentar
dpb
dpb am 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

dpb
dpb am 22 Jun. 2023
Bearbeitet: dpb am 22 Jun. 2023
Hint:
fnames={'378','933','934','935'}; % simulate array of filenames
nums=str2double(fnames) % convert to numeric
nums = 1×4
378 933 934 935
ix=find(diff(nums)>1)+1 % the location series breaks
ix = 2
c{1}=fnames(1:ix-1);
c{2}=fnames(ix:end);
c{:}
ans = 1×1 cell array
{'378'}
ans = 1×3 cell array
{'933'} {'934'} {'935'}
Extension to more than one set of breakpoints straightforward with looping construct.
The issue of defining a broken series but how far apart the difference must be to be within the same series is similar; you define both an upper limit for the magnitude difference as well that the difference must be within to be ignored.
The most excellent FEX submission from @Jan <runlength> packages the above for you very nicely although it does not filter the missing within a sequence for you; you'll have to screen its results for that feature.

Weitere Antworten (0)

Produkte


Version

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by