Merge specific text files
显示 更早的评论
I have a code that for each iteration creates specific files with names :
M1_mR1.txt,...,M1_mr100.txt, and
M2_mR1.txt,..., M2_mR100.txt.
After that I would like to merge all the files that Have prefix M1_.....txt into one file and all the files that Have prefix M2_.....txt into one file.
How could I do this?
采纳的回答
更多回答(1 个)
KSSV
2020-7-6
% to read files starting with M1
txtFiles = dir("M1*.txt") ;
N = length(txtFiles) ;
A = zeros(N,4) ;
for i = 1:N
a = importdata(txtFiles(i).name) ;
A(i,:) = a ;
end
4 个评论
Ivan Mich
2020-7-6
KSSV
2020-7-6
Yes make loop for each M1, M2 and M3.
Ivan Mich
2020-7-6
Note that this answer will concatenate the file data in this order:
M1_mR1.txt
M1_mR10.txt
M1_mR100.txt
M1_mR11.txt
M1_mR12.txt
M1_mR13.txt
M1_mR14.txt
M1_mR15.txt
M1_mR16.txt
M1_mR17.txt
M1_mR18.txt
M1_mR19.txt
M1_mR2.txt
M1_mR20.txt
M1_mR21.txt
M1_mR22.txt
...
M1_mR89.txt
M1_mR9.txt
M1_mR90.txt
M1_mR91.txt
M1_mR92.txt
M1_mR93.txt
M1_mR94.txt
M1_mR95.txt
M1_mR96.txt
M1_mR97.txt
M1_mR98.txt
M1_mR99.txt
which is unlikely to be very useful for further analysis.
To import the files in order 1,2,3,...100 you will either need to sort the filenames taking into account the number value (e.g. using an alphanumeric sort) or generate the filenames yourself (e.g. using sprintf).
类别
在 帮助中心 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!