Create folders in matlab and move into files via loop

Hello,
I am making via a loop 4 .txt files. I would like to create 4 files and move each one of 4.txt files in these 4 folders (I mean each folder would have one .txt files).
How could I make it?

Réponses (1)

You may try the following
for i =1:4
currentFolder = sprintf('Folder%d',i);
mkdir(currentFolder)
cd(currentFolder)
fileID = fopen('FileName.txt','w');
% Write to file
cd ..
end

6 commentaires

Ok, thank you , but the point is that my loop creates the files I want, I would like before making the next file to put it into the folder. I mean that the files are not empty.
Do you know what I have to do?
You may still use this code. Add the code related to creation of files at the place of Create the file comment.
for i =1:4
currentFolder = sprintf('Folder%d',i);
mkdir(currentFolder)
cd(currentFolder)
% Create the file
cd ..
end
If this doesn't help, Can you share your loop?
the loop is :
for n=1:numel(st);
for i=1:size(nam);
FP=fopen(sprintf('m%g0.txt',i),'wt');
fprintf(FP,'%s\t',num2str(Results));
fclose(FP);
D = 'absolute/relative path to where the files are saved';
N = 25; % number of files
C = cell(1,N);
for k = 1:N
F = fullfile(D,sprintf('m%u.txt',k));
C{k} = dlmread(F);
end
M = vertcat(C{:});
dlmwrite('final.txt',M,'\t')
end
end
To sum up I would like to create multiple folders, in each one to be contained 1 final .txt file, after each iteration (eg two folders two and each folder contains a final.txt file).
Try the following code
for n=1:numel(st);
for i=1:size(nam);
% Uncomment below line to create folder for each m*.text file
% currentFolder = sprintf('Folder%d%d',n,i);
% mkdir(currentFolder)
% cd(currentFolder)
FP=fopen(sprintf('m%g0.txt',i),'wt');
fprintf(FP,'%s\t',num2str(Results));
fclose(FP);
% cd ..
D = 'absolute/relative path to where the files are saved';
N = 25; % number of files
C = cell(1,N);
for k = 1:N
F = fullfile(D,sprintf('m%u.txt',k));
C{k} = dlmread(F);
end
M = vertcat(C{:});
% Create a folder for each final.txt
currentFolder = sprintf('FinalFolder%d%d',n,i);
mkdir(currentFolder)
cd(currentFolder)
dlmwrite('final.txt',M,'\t')
cd ..
end
end
ok, one question. Is there any way to replace dlmwritte with writematrix? I think that the problem is here
Yes, you may replace dlmwrite with writematrix
writematrix(M,'final.txt','Delimiter','tab')
Refer the below links for replacing dlmwrite and dlmread with writematrix and readmatrix respectively

Connectez-vous pour commenter.

Catégories

En savoir plus sur Characters and Strings dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by