How can I run this script and only create folders with files in it without having an additional file outside the folder?

1 vue (au cours des 30 derniers jours)
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);

Réponse acceptée

Stephen23
Stephen23 le 13 Sep 2021
Modifié(e) : Stephen23 le 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);

Plus de réponses (0)

Catégories

En savoir plus sur File Operations dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by