Add a loop and create .txt files.

3 views (last 30 days)
Ivan Mich
Ivan Mich on 4 Jul 2020
Edited: Ivan Mich on 4 Jul 2020
Hello,
I have a question about a code.
Well, my code creates by a loop multiple .txt files with specific name and format, if I use one loop in order to make my calculations. But, I would like to add one more loop in order to make my code better.
I am giving you my code here:
name=regexp(fileread('names.txt'), '\r?\n', 'split') .';
file1=regexp(fileread('data.txt'), '\r?\n', 'split') .';
.....
for n=1:numel(name);
for z=1:size(file1)
........ %making caluclations
FP=fopen(sprintf('mR%g0.txt',z),'wt');
fprintf(FP,'%s\t',file1{z},num2str(number),num2str(number);
fclose(FP);
txtFiles = dir('mR*.txt') ; % get the text files in the present folder
N = length(txtFiles) ; % Total number of text files
iwant = cell(N,1) ; % initlaize the data required
% loop for each file
for i = 1:N
thisFile = txtFiles(i).name ;
iwant{i} = importdata(thisFile) ; % read data of the text file
end
iwant = cell2mat(iwant) ;
outFile = strcat('finalR',num2str(n),'.txt') ;
dlmwrite(outFile,iwant,'delimiter','\t')
end
end
My code:
1)creates multiple mR.txt files
2)merge all multiple mR.txt files into one file, called finalR.txt
So I would like to add a for loop, which:
1) will read a file (lets call it "file2.txt") with multpile lines
2)Creates for each iteration of file2.txt mulptle files( eg M_1mR*.txt for the first iteration, M2_mR*txt for the second iteration etc)
3)Merges files M_1mR*.txt to one file , M_2mR*.txt to one file etc.
I have tried these:
for w=1:size(file1)
for z=1:size(file1)
.......
txtFiles = dir('M_*',w,'mR*.txt',z) ; % get the text files in the present folder
end
end
but it didn't work...
Could anyone help me?

Answers (0)

Categories

Find more on Characters and Strings 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!