Create a hierarchy of folders and files by using matlab the code for i=1:n Name=
ID: 3883629 • Letter: C
Question
Create a hierarchy of folders and files
by using matlab
the code
for i=1:n
Name=['dir',num2str(i)];
mkdir(Name);
for j=1:m
M = magic(7);
Name1=['file',num2str(j),'.txt'];
dlmwrite([Name,'',Name1],M,'-');
end
end
Explanation / Answer
%Matlab program to create hierarchy of files and folders
n=input(' Enter the maximum number of directories:'); % input 'n' the number of directories to create
m = input(' Enter the maximum number of files:'); % input 'm' the number of files to create in each directory
% loop to create directories
for i=1:n
dir_name=strcat('dir',num2str(i)); % initializing dir_name variable with the required name of the directory
mkdir(dir_name); % creating directories
% loop to create files in the directories
for j=1:m
file_name = strcat('file',num2str(j),'.txt'); % initializing file_name variable with the required name of the file
filename = fullfile(dir_name,file_name); % buildng full filename u.e directory name + file name
file = fopen(filename,'w'); %opening the file in write mode
dlmwrite(filename,magic(j),'delimiter','-'); % write matrix magic(j) to ASCII-delimited file with delimiter '-' fclose(file); % close the file
end
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.