Add header to data file

2 次查看(过去 30 天)
monkey_matlab
monkey_matlab 2016-1-7
Hello,
In my code below, I wanted to know how to go about adding a header to the file. The header will be the column names:
A B C D E. Thanks for your time and help. Here is my code:
A = 1:15;
B = (0.5)*rand(15,1);
C = 1:20;;
D = (0.5)*rand(20,1);
E = (0.5)*rand(20,1);
CELL{1} = A';
CELL{2} = B;
CELL{3} = C';
CELL{4} = D;
CELL{5} = E;
Astr = cell(20,5);
Astr = cellstr(num2str(A.', '%6.0f'));
Output(1:length(Astr), 1) = Astr;
Astr = cellstr(num2str(B, '%6.5f'));
Output(1:length(Astr), 2) = Astr;
Astr = cellstr(num2str(C.', '%6.0f'));
Output(1:length(Astr), 3) = Astr;
Astr = cellstr(num2str(D, '%6.5f'));
Output(1:length(Astr), 4) = Astr;
Astr = cellstr(num2str(E, '%6.5f'));
Output(1:length(Astr), 5) = Astr;
fileID = fopen('check.dat','wt');
fmt = '%s,%s,%s,%s,%s\n';
OutTranspose = Output .'; %need to transpose it
fprintf(fileID, fmt, OutTranspose{:});
fclose(fileID);

回答(1 个)

Walter Roberson
Walter Roberson 2016-1-7
fprintf(fileID, fmt, CELL{:});

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by