How can I run this script and only create folders with files in it without having an additional file outside the folder?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
ARAH MIZORI
am 13 Sep. 2021
Bearbeitet: Stephen23
am 13 Sep. 2021
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 Kommentare
Akzeptierte Antwort
Stephen23
am 13 Sep. 2021
Bearbeitet: Stephen23
am 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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Text Files finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!