How can I run this script and only create folders with files in it without having an additional file outside the folder?
1 view (last 30 days)
Show older comments
clc
clear
close all
% start timer
start = tic;
% input n and m
n = input('\n How many folders would you like me to make?:');
m = input('\n How many files per folder would you like me to make?:');
% create for loop to make folders and files
for i=1:n
foldername=strcat('dir',num2str(i)); % name folder
mkdir(foldername); % creates folder
for j=1:m
filename = strcat('file',num2str(j),'.txt'); % name file
filename1 = fullfile(foldername,filename); % build a full file
file = fopen(filename1,'w'); % open file in write mode
writematrix(magic(j),filename,'delimiter','|'); % write matrix in txt format with | as delimiter
fclose(file); % close file
end
end
% stop timer
stop = toc(start);
% display the recorded time
fprintf('\n Execution time : %f seconds \n',stop);
0 Comments
Accepted Answer
Stephen23
on 13 Sep 2021
Edited: Stephen23
on 13 Sep 2021
Get rid of the FOPEN and FCLOSE, they are completely unrelated to WRITEMATRIX:
start = tic;
% input n and m
n = input('\n How many folders would you like me to make?:');
m = input('\n How many files per folder would you like me to make?:');
% create for loop to make folders and files
for ii = 1:n
foldername = strcat('dir',num2str(ii)); % name folder
mkdir(foldername); % creates folder
for jj = 1:m
filename = strcat('file',num2str(jj),'.txt');
fullname = fullfile(foldername,filename);
writematrix(magic(jj), fullname, 'delimiter','|');
end
end
% stop timer
stop = toc(start);
% display the recorded time
fprintf('\n Execution time : %f seconds \n',stop);
0 Comments
More Answers (0)
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!