Check the existence of a directory based on part of a string?
Ältere Kommentare anzeigen
hello all,
I have a range of sub-folders which are named as CL_CS, CL_CS_01, CL_CS_02...CL_CS_xx. These folders are located in same directory with other folders. I want to run a loop which checks the first 5 characters of the folder name and run some operations inside those folders.
I am not quite sure how to match the string for a folder name. So far...
fpath = 'CL_CS';
while isequal(exist(fpath,'dir'),7)
% do something here
end
Thanks!
Akzeptierte Antwort
Weitere Antworten (1)
Azzi Abdelmalek
am 24 Aug. 2016
When you run
f=dir(current_folder)
You wil get a cell array of the content of your current folder, like
f={'CL_CS','CL_CS_01','CL_CS_02','other1' 'other'}
name=regexp(f,'CL_CS.+','match','once')
idx=~cellfun(@isempty,name)
name=name(idx)
4 Kommentare
automycer
am 24 Aug. 2016
Azzi Abdelmalek
am 24 Aug. 2016
f={f.name}
@automycer: see my answer, I already showed you how to do this correctly (Azzi Abdelmalek's answer is not correct, because as you have found dir returns a structure, not a cell array of names).
And I also showed you how to get the names into the correct order!
Azzi Abdelmalek
am 24 Aug. 2016
@Stephen Cobeldick, why the answer is not correct? dir returns a struct variable, but you can get the cell array containing the names, as mentioned in my above comment!
Kategorien
Mehr zu Dates and Time finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!