generate multiple text files from a matrix
显示 更早的评论
Hi all,
i have a 1000 x 2 matrix and i want to generate 1000 text files with two lines with first entry from first colum of matrix and second from second (for first files and so on till 1000). Will appreciate any help in this regard
a = 1...1000
b = 1001...2000
txt1= (1,10001)
....
......
txt1000 = (1000, 2000)
采纳的回答
更多回答(1 个)
Jon
2021-1-15
While I was working on this I see you have already received an answer. This is probably similar to what Mathieu has already submitted, but since I had already spent time preparing it I'll provide it as an alternative. I like to put leading zeros on my filenames e.g. txt00001.csv, txt0002.csv, ... txt0999.csv, txt1000.csv as you can then easily retrieve them in alphabetical (dictionary) order.
baseName = 'txt'
A = rand(1000,2)
% loop through data making individual files for each row
numRows = size(A,1);
for k = 1:numRows
% build filename % e.g. txt083.csv
filename = [baseName,num2str(k,'%04d'),'.csv'];
% write data to file
writematrix(A(k,:),filename)
end
1 个评论
Jon
2021-1-15
By the way, I wonder why you are creating so many smaill files. Often there is a more efficient alternative, but I can't offer any suggestions without knowing what your overall problem is
类别
在 帮助中心 和 File Exchange 中查找有关 Variables 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!