Make the subfolders name in output folder the same input folder

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 comentarios

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?
Yes. after the process, input has "output_data_*.dat" file and other files in any subfolder, but on "output" folder is clear. I want have the same subfolder on "output" folder and have only "output_data_*.dat"

Iniciar sesión para comentar.

 Respuesta aceptada

Stephen23
Stephen23 el 3 de Nov. de 2018
Editada: Stephen23 el 3 de Nov. de 2018
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 comentarios

Long Hà Vinh
Long Hà Vinh el 3 de Nov. de 2018
Editada: Stephen23 el 3 de Nov. de 2018
It can not dir the file.
S =
0×1 empty struct array with fields:
name
folder
date
bytes
isdir
datenum
I also remove '*'
[S = dir(fullfile(D,'input','*'));]
to
[S = dir(fullfile(D,'input'));]
but result the same.
Stephen23
Stephen23 el 3 de Nov. de 2018
Editada: Stephen23 el 3 de Nov. de 2018
@Long Hà Vinh: you need to ensure that the path is correct, and that you have the correct filepath: you can use exist. Note that you did not explain your aims very clearly: you appear to search for subdirectories in a particular location, so check those locations and the folders really exist in that location.
I change the command to
S = dir('~/Matlab/homework_4/input')
and it running. Maybe something wrong with my Matlab Linux-version. I will re-checked on the windows version.
Stephen23
Stephen23 el 3 de Nov. de 2018
Editada: Stephen23 el 3 de Nov. de 2018
Does this give the same output?:
D = '~/Matlab/homework_4';
S = dir(fullfile(D,'input'));
I general it the same, but I dont know why in my case when I run
S = dir('~/Matlab/homework_4/input')
Said:
S =
4×1 struct array with fields:
name
folder
date
bytes
isdir
datenum
but when I run your way
D = '~/Matlab/homework_4';
S = dir(fullfile(D,'input'));
it said:
S =
0×1 empty struct array with fields:
name
folder
date
bytes
isdir
datenum
I complete test by copy your code and test both on command and editor, but S = 0 x 1 still happening. That why I said "I need to test with windows version of Matlab". Thanks for your support and I reply asap when I finish the test.
@Long Hà Vinh: weird. What is the output from this?:
fullfile(D,'input')
@Stephen: It said:
'~/Matlab/homework_4/input'
when I run in command. But when I run
E = fullfile(D,'input')
S = dir(E)
It break and when I call
E=
'~/Matlab/homework_4/input'
S =
0×1 empty struct array with fields:
name
folder
date
bytes
isdir
datenum
Sound crazy!
@Stephen Cobeldick: This completely work on windows version. So I need to fix my Matlab on Linux. Thanks for your support!

Iniciar sesión para comentar.

Categorías

Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.

Productos

Versión

R2017b

Preguntada:

el 3 de Nov. de 2018

Comentada:

el 3 de Nov. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by