Writing matrix to .dat file

73 次查看(过去 30 天)
I need to write two sets of data to a .dat file. I have two row vectors, n and an. The index n needs to be written to the value of an. When I attempt this, though, I get all of the n values first and then the an values when I need them in two columns. Here's what I tried.
% Transforming vectors into column vector formatting
n = 0:50;
n = n'
% Creating matrix to be called and written to .dat
mat = [n,an];
%%Exporting data to .dat
% Creating file to be written to
fileName = fopen('mat.dat','w');
% Writing data to file
fprintf(fileName, '%f %f\n', mat);
% Closing
fclose(fileName)
%Opening file to read
type mat.dat
I'm not too familiar with this so I'm sure I'm missing something crucial.

采纳的回答

Walter Roberson
Walter Roberson 2018-8-29
fprintf(fileName, '%f %f\n', mat.');
This is due to the fact that MATLAB accesses data "down" the columns first.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Standard File Formats 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by