How to write data with timestamps into a CSV file in MATLAb 8.0 (R2012b)?
14 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
MathWorks Support Team
le 18 Oct 2013
Réponse apportée : MathWorks Support Team
le 16 Jan 2014
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 ?
Réponse acceptée
MathWorks Support Team
le 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) ;
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Standard File Formats dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!