create separate cells based on the (numbered) files contained in a folder
1 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
dpb
2023 年 6 月 22 日
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.
採用された回答
dpb
2023 年 6 月 22 日
編集済み: dpb
2023 年 6 月 22 日
Hint:
fnames={'378','933','934','935'}; % simulate array of filenames
nums=str2double(fnames) % convert to numeric
ix=find(diff(nums)>1)+1 % the location series breaks
c{1}=fnames(1:ix-1);
c{2}=fnames(ix:end);
c{:}
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.
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で File Operations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!