Writing file in matlab with fprintf and dlmwrite in loop.
Show older comments
I'm trying to get a loop to write headers between matrices to a .txt file from matlab.
Example code:
filename = 'sampleXLS.dat';
datfile = fopen(filename,'w');
header = 'Header \n';
mat1 = [1,2,3;4,5,6];
mat2 = [7,8;9,10];
mat3 = [11;12;13];
mats = {mat1,mat2,mat3};
for iter = 1:length(mats)
data = mats{iter}
fprintf(datfile, header);
dlmwrite(filename, data,'-append','delimiter',' ','precision', '%f');
end
I want
Header
1 2 3
4 5 6
Header
7 8
9 10
Header
11
12
13
I am getting
Header
Header
Header
3
4 5 6
7 8
9 10
11
12
13
Note the missing 1 and 2, when I step though the file in debug mode it is inserting and replacing the numbers with the headers as it goes along.
Is there a way to tell fprintf to append?
Accepted Answer
More Answers (0)
Categories
Find more on Geometry and Mesh in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!