Write table with several header lines to file
22 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
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 个评论
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 Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!