필터 지우기
필터 지우기

How to make directories within directories within directories

조회 수: 57 (최근 30일)
Mike
Mike 2018년 5월 21일
댓글: Guillaume 2018년 5월 21일
Hi,
The first loop 'for i=1..' creates 3 directories, '1,2,3'. The second loop, 'for n=1:..', then creates two directories inside each of them called 'a' and 'b'. The code is successful thus far. The final loop (list3) attempts to create two further directories 'q' and 'r' within 'a' and 'b'. This loop produces another a,b containing q,r, which is fine, but at the same level as 1,2,3 which is not. So I obtain mainfolder\(1&2&3)\(a&b) + mainfolder\(a&b)\(q+r), rather than the required mainfolder\(1&2&3)\(a&b)\(q+r). I was attempting to use mkdir as in the example: mkdir(Parentfolder, foldername) which creates the foldername within Parentfolder. Thanks in advance! Sorry for the long winded explanation and awkward notation
function testforum();
list1=["1","2","3"]
list2=["a","b"]
list3=["q","r"]
for i=1:length(list1)
x=list1(i)
mkdir(sprintf('%s',x))
for n=1:length(list2)
y=list2(n)
mkdir (sprintf('%s',x),sprintf('%s',y))
for p=1:length(list3)
z=list3(p)
mkdir (sprintf('%s',y),sprintf('%s',z))
end
end
end
end
  댓글 수: 2
Jan
Jan 2018년 5월 21일
Using the string type makes the code more complicated than necessary. The pile of sprintf commands are confusing.
Guillaume
Guillaume 2018년 5월 21일
string doesn't complicate anything. mkdir, fullfile, etc. are all happy with strings.

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

채택된 답변

Jan
Jan 2018년 5월 21일
편집: Jan 2018년 5월 21일
Use absolute paths for the folders. You cannot create a subfolder using the mkdir(parent, sub) notation, if parent is not found. Example:
base = 'D:\Temp'; % Assumed to be existing
cd(base);
mkdir('A'); % C:\Temp\A created
mkdir('A', 'B'); % C:\Temp\A\B created, because 'A' is found in current directory
mkdir('B', 'C'); % This creates C:\Temp\B\C, not C:\Temp\A\B\C
mkdir('A\B', 'C'); % This creates C:\Temp\A\B\C
Much easier:
mkdir(fullfile(base, 'A', 'B', 'C'))
Use full path names to avoid any troubles. The notation mkdir(parent, sub) was needed in Matlab R5.3, but after 20 years I suggest to use the modern power of mkdir to create a complete folder tree at once.
A fixed version of your code:
base = 'D:\Temp'; % Set as needed
list1 = {'1', '2', '3'}
list2 = {'a', 'b'}
list3 = {'q', 'r'}
for i = 1:length(list1)
x = list1{i}
for n = 1:length(list2)
y = list2{n}
for p=1:length(list3)
z = list3{p}
mkdir(fullfile(base, x, y, z));
end
end
end
  댓글 수: 2
Mike
Mike 2018년 5월 21일
Thank you very much!
Guillaume
Guillaume 2018년 5월 21일
I'll reiterate what I said in my answer because it's important: Numbered variables are always an indication of a flawed design. Note that x, y, z is the same as numbered variables. Any kind of sequential naming is indication of a flawed design.

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

추가 답변 (1개)

Guillaume
Guillaume 2018년 5월 21일
Numbered variables are always an indication of a flawed design. Your algorithm is not easily extensible to adding more levels to your directory structure since it means adding another numbered variable and another for loop.
It would be much simpler to have a single variable and a recursion or a single loop. But since mkdir will create all the directories necessary, I would just build all the full paths and pass that in a loop:
lists ={["1","2","3"], ["a","b"], ["q","r"]};
%build the cartesian product of elements of lists:
cartprod = cell(size(lists));
[cartprod{:}] = ndgrid(lists{:});
%pass the cartesian product to fullfile to build the paths
allpaths = fullfile(cartprod{:});
%now iterate over all the paths
for pidx = 1:numel(allpaths)
mkdir(allpaths(pidx)); %will create all inexistant directories in the path
end
The above works with any number of levels, just add the extra level(s) to lists, up to the combinatorial limit of ndgrid.

카테고리

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