MATLAB Answers

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

3 ビュー (過去 30 日間)
ARAH MIZORI
ARAH MIZORI 2021 年 9 月 13 日
編集済み: Stephen 2021 年 9 月 13 日
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);

採用された回答

Stephen
Stephen 2021 年 9 月 13 日
編集済み: Stephen 2021 年 9 月 13 日
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 件)

Community Treasure Hunt

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

Start Hunting!

Translated by