Add a loop and create .txt files.
3 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!