필터 지우기
필터 지우기

Write table with several header lines to file

조회 수: 22 (최근 30일)
Michael
Michael 2017년 2월 27일
댓글: Guillaume 2017년 2월 27일
I would like to write a csv-file with the delimiter ";" and several header lines. The csv-file shold look like the following example in the end:
Header line 1 (string 1)
Header line 2 (string 2)
UTC [yyyy-mm-dd HH:MM:SS.fff] ; vx [m/s] ; vy [m/s] ; vz [m/s]
2017-02-11 23:00:00.000 ; -2.3059 ; 0.2341 ; 0.1177
2017-02-11 23:00:00.002 ; -2.5948 ; 0.1642 ; -0.0821
2017-02-11 23:00:00.004 ; -2.5439 ; 0.2497 ; 0.0270
. . .
I use the following code:
header={'Header line 1 (string 1)' 'Header line 2 (string 2)' 'UTC [yyyy-mm-dd HH:MM:SS.fff]' 'vx [m/s]' 'vy [m/s]' 'vz [m/s]'};
v=Data; % N×3 double matrix of measured velocity values
t=UTCtime; % N×1 datetime array
output_file_csv=filepath; % path and file name of output file
fid = fopen(output_file_csv, 'wt');
for l = 1:numel(header)
fprintf(fid, '%s\n', header{l});
end
fclose(fid);
M={t,v};
dlmwrite(output_file_csv, M, '-append', 'delimiter', ';');
This code created the error message
Error using dlmwrite (line 112)
The input cell array cannot be converted to a matrix.

답변 (1개)

Guillaume
Guillaume 2017년 2월 27일
headerlines = {'AG A&K, IG KlimAT';
'HBI, 2017-02-23 03:46:04';
'+QSLX 100.7';
'Zeit ; v3Dx_QSLX100_7_ESTO ; v3Dy_QSLX100_7_ESTO ; v3Dz_QSLX100_7_ESTO';
'UTC [yyyy-mm-dd HH:MM:SS.fff] ; Stroemungsgeschwindigkeit_3Dx [m/s] ; Stroemungsgeschwindigkeit_3Dy [m/s] ; Stroemungsgeschwindigkeit_3Dz [m/s]'};
filepath = 'somefile.txt';
fid = fopen(filepath, 'wt');
for l = 1:numel(headerlines)
fprintf(fid, '%s\n', headerlines{l});
end
fclose(fid);
dlmwrite(filepath, yourmatrix, '-append', 'delimiter', ';'); %with optional 'newline', if you wish
  댓글 수: 2
Michael
Michael 2017년 2월 27일
편집: Michael 2017년 2월 27일
Thank you for your quick answer. Unfortunately dlmwrite does not work (see edited question above).
Guillaume
Guillaume 2017년 2월 27일
You probably can do it with xlswrite but at this point, I'd simply do it with low level functions
fid = fopen(filepath, 'wt');
for l = 1:numel(headerlines);
fprintf(fid, '%s\n', headerlines{l});
end
UTCtime.Format = 'yyyy-MM-dd HH:mm:ss.SSS'; %May not be needed if UTCtime already use the correct format.
out = [num2cell(UTCtime), num2cell(Data)].'; %put it all in a cell array
fprintf(fid, '%s; %f; %f; %f\n', out{:});
fclose(fid);

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by