Make the subfolders name in output folder the same input folder
조회 수: 1 (최근 30일)
이전 댓글 표시
I have a data process folder like this: with directory ~/Matlab/homework_4/input

I already access into the folder to sub-folder to do my job by the code:
d = dir(pwd);%d(1:2) = [];
isub = [d(:).isdir]; %# returns logical vector
fname = {d(isub).name}';
fname(ismember({d.name}, {'.', '..'})) = []; %remove . and .. of folder name.
% ------------------------------------------------------------
for i = 1:length(fname)
cd(char(fname{i})
<[........ Do the job .......]
output_data.dat>
end
and wanna create the same-name of sub-folder on the directories: "~/Matlab/homework_4/output" using "fname{i}" on the code. And move the output_data.dat to that folder. Any way to do this? Many thanks :(
댓글 수: 2
awezmm
2018년 11월 3일
So basically, you want to have a copy of ~/Matlab/homework_4/input but instead, name it ~/Matlab/homework_4/output with all the same subfolders inside?
채택된 답변
Stephen23
2018년 11월 3일
편집: Stephen23
2018년 11월 3일
Do NOT use cd: this is a slow and inappropriate for reading/writing data. You should just use absolute/relative filenames generated with fullfile, e.g.:
D = '~/Matlab/homework_4';
S = dir(fullfile(D,'input','*'));
N = setdiff({S([S.isdir]).name},{'.','..'});
for k = 1:numel(N)
src = fullfile(D,'input',N{k}) % input subfolder path
I = imread(fullfile(src,'hello.jpg')); % example file import
... process the image.
dst = fullfile(D,'output',N{k}) % output subfolder path
imwrite(I,fullfile(dst,'world.jpg')) % example file export
end
All MATLAB functions that import/export files work with relative/absolute filenames, and this is faster, more efficient, and easier to debug than mucking about with changing directories.
댓글 수: 8
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!