How to write data with timestamps into a CSV file in MATLAb 8.0 (R2012b)?

36 views (last 30 days)
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 ?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 18 Oct 2013
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) ;

More Answers (0)

Products


Release

R2012b

Community Treasure Hunt

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

Start Hunting!