How to write data with timestamps into a CSV file in MATLAb 8.0 (R2012b)?
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
MathWorks Support Team
am 18 Okt. 2013
Beantwortet: MathWorks Support Team
am 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 ?
Akzeptierte Antwort
MathWorks Support Team
am 18 Okt. 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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Standard File Formats finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!