Borrar filtros
Borrar filtros

Add header to data file

1 visualización (últimos 30 días)
monkey_matlab
monkey_matlab el 7 de En. de 2016
Respondida: Walter Roberson el 7 de En. de 2016
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);

Respuestas (1)

Walter Roberson
Walter Roberson el 7 de En. de 2016
fprintf(fileID, fmt, CELL{:});

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by