필터 지우기
필터 지우기

Add header to data file

조회 수: 1 (최근 30일)
monkey_matlab
monkey_matlab 2016년 1월 7일
답변: Walter Roberson 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