Changing Current directory (cd) using a string variable.

조회 수: 22 (최근 30일)
Christien Bowman
Christien Bowman 2020년 3월 12일
편집: Christien Bowman 2020년 3월 12일
I'm trying to change directory into two levels of subfolders but am having trouble making cd(variable) accept the input argument.
Once into each subfolder, I need to call a function (dirf) that will create a batch file of all files in the cd.
Thanks!
%find directory
p = uigetdir
% Get a list of all files and folders in this folder ie: birds
files = dir;
names = {files.name};
% Get a logical vector that tells which is a directory.
dirFlags = [files.isdir] & ~strcmp(names, '.') & ~strcmp(names, '..');
% Extract only those that are directories. . and .. are up and down folder
% commands which is why they are removed
subDirsNames = names(dirFlags);
%gets data on how many subfolders in the cd there are
N = numel(subDirsNames)
% for loop for entering into each subdir
for ii=1:N
% selects subfolder ie: single bird folder
cdm = fullfile(p,subDirsNames(ii))
cdm = cell2mat(cdm)
cd(cdm)
% same logical process filter as before but for sub-sub folders. remove
% this part if you only have single level folders
subsubdir = dir;
subnames = {subsubdir.name}
subdirFlags = [subsubdir.isdir] & ~strcmp(subnames, '.') & ~strcmp(subnames, '..');
subsubDirsNames = subnames(subdirFlags);
NN = numel(subsubDirsNames)
% selects sub subfolder ie: timepoint or FD song/UD song
for jj=1:NN
cds = fullfile(p,subDirsNames(ii),subsubDirsNames(jj))
cds = cell2mat(cdm)
cd(cds)
dirf('*.wav','batch')
end
end
  댓글 수: 2
Stephen23
Stephen23 2020년 3월 12일
편집: Stephen23 2020년 3월 12일
@Christien Bowman : do you have a specific problem or question? You have shown us some code, but you have not asked us anything, nor given any error messages or warning that you might be getting.
Rather than messing around with slow cd, it would be simple and efficient to access data files using absolute/relative filenames. All MATLAB functions that read/write to data files accept absolute/relative filenames.
Note you can eliminate the . and .. folders in one line:
files = dir();
names = setdiff({files([files.isdir]).name},{'.','..'})
Christien Bowman
Christien Bowman 2020년 3월 12일
편집: Christien Bowman 2020년 3월 12일
Thank you! That helps with the efficiency a lot.
I restarted MATLAB and 'cd' appears to be working correctly now.

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

답변 (1개)

Walter Roberson
Walter Roberson 2020년 3월 12일
files = dir;
I think you mean
files = dir(p) ;

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by