How to write data with timestamps into a CSV file in MATLAb 8.0 (R2012b)?
12 次查看(过去 30 天)
显示 更早的评论
MathWorks Support Team
2013-10-18
回答: MathWorks Support Team
2014-1-16
I have my data which includes time. I want to write this as a csv-file, with the timestamp in the first column and then the data after that. How can I do this ?
采纳的回答
MathWorks Support Team
2013-10-18
CSVWRITE can only write numeric values.
DLMWRITE can only write either numeric or character data on any one call (and character is not recommended.)
XLSWRITE can handle cell arrays, but only when you are using MS Windows and have Excel installed; this would normally be used for writing .xls or .xlsx files and I do not know if you could write csv files with it.
Thus you can use low level I/O to achieve this functionality, as seen in the below code:
%%Create example input data
Data = rand(10, 2) ;
% Cell array with time values
timeValue = datenum(2011, 12, 1:10) ; % Time in Matlab format
for iTime = 1:length(timeValue)
Time{iTime} = datestr(timeValue(iTime), 'yyyy-mm-dd-HH-MM') ;
end
%%Write CSV file
fid = fopen('c:\temp\test.csv', 'w') ;
for iLine = 1:size(Data, 1) % Loop through each time/value row
fprintf(fid, '%s1,', Time{iLine}) ; % Print the time string
fprintf(fid, '%12.3f, %12.3f\n', Data(iLine, 1:2)) ; % Print the data values
end
fclose(fid) ;
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Standard File Formats 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!