About NotDirectoryError (Caught "std::exception" Exception message is:)

조회 수: 5 (최근 30일)
Yoonjung Lee
Yoonjung Lee 2015년 1월 28일
댓글: Cedric 2015년 1월 28일
Hello, I am using SPM8 with matlab. I have an error as below so that 'for' statement does not work.
Caught "std::exception" Exception message is: fl:filesystem:NotDirectoryError
Whenever I run the script, I also have "matlab syntax error cannot run as it appears" too.
I made simple scripts to remove unnecesarry files in each directory. There are subfolders named 's01', 's02' in the root directory. I wanted to remove unnecessary files whose name starts with 'main', 'p', 'ap', in each subfolders. I realized that this script always works for the "first" subfolders such as 's01', but it does not work for the "second (s02)" or ... subfolders. Could anyone advise me, please?
SN = {}; SN{end+1} = {'s01', 1:6}; SN{end+1} = {'s02', 1:6};
root_dir = '/home/data/PRES';
%% Deleting unnecessary files
for mm=1:size(SN, 2); aa=dir(fullfile(root_dir, SN{mm}{1}));
for nn=1:size(aa,1)
delete(fullfile(root_dir, SN{mm}{1}, aa(nn).name, 'main*'));
delete(fullfile(root_dir, SN{mm}{1}, aa(nn).name, 'p*'));
delete(fullfile(root_dir, SN{mm}{1}, aa(nn).name, 'ap*'));
end
end

답변 (1개)

Cedric
Cedric 2015년 1월 28일
편집: Cedric 2015년 1월 28일
In your calls to delete:
SN{mm}{1} is the folder name.
aa(nn).name is the file name.
so why are appending e.g. 'main*' ? You will get path/names like
/home/data/PRES/s01/main_abc.txt/main*
which is not what you want. You should either use the pattern 'main*' when you build the call to DIR, e.g. (untested)
SN = {{'s01', 1:6}, {'s02', 1:6}} ;
patterns = {'main*', 'p*', 'ap*'} ;
root_dir = '/home/data/PRES' ;
for dirId = 1 : numel( SN )
dirPath = fullfile( root_dir, SN{dirId}{1} ) ;
if ~exist( dirPath ), continue ; end
for patId = 1 : numel( patterns )
content = dir( fullfile( dirPath, patterns{patId} )) ;
for fileId = 3 : numel( content ) % 1 is '.', 2 is '..'
delete( content(fileId).name ) ;
end
end
end
or loop over all files and match relevant files according to patterns.
  댓글 수: 2
Cedric
Cedric 2015년 1월 28일
You're welcome! Please note that I probably updated my answer while you were writing your comment, so take the latest version.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Search Path에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by