How do I combine datetime and double precision decimal data in one text file?

11 views (last 30 days)
I have several separate column vectors with data in double precision format, and one column vector (Tm) with time in datenum format. I want to convert datenum to datetime and save the corresponding data to one text file. I have tried to do this with one of the datasets, m_sst, with the code below:
%Open a text file
fileID = fopen('allData.txt', 'w');
%Assign the column headings
fprintf(fileID, '%6s %12s %18s\n', 'Date', 'Time', 'SST');
%Vectorise the computation
t = datetime(Tm, 'ConvertFrom', 'datenum');
%Append data to the end of the file
fileID = fopen('allData.txt', 'a');
%Match the data to the column headings
fprintf(fileID, '%s %s %18.6f\n', t, m_sst);
I get an error message at the second fprintf command:
Error using fprintf
Unable to convert 'datetime' value to 'double'.
This suggests to me that my script is trying to coerce all the data (including datetime) to double precision, when only m_sst should be double precision. However, I don't know how to correctly designate the datetime info.

Accepted Answer

dpb on 2 Nov 2018
Edited: dpb on 3 Nov 2018
Your script is, indeed trying to do that because of the way fprintf works.
fprintf(fileID, '%s %s %18.6f\n', t, m_sst);
will first output array t, then array m_sst with the format string. When it gets to the '%f' with the time array third element, it fails. There's another issue regarding memory order; and you can't concatenate a datetime with a double into an array to deal with that.
Instead, write:
fmt='%s '%18.6f\n';
for i=1:size(t,1)
NB: You only need one '%s' for the datetime; set the .Format property to display it as you want and ML will convert the internal representation as a single string.
There is another way with combo data that is handy if you're willing to accept less control over the form of the output--put the data into a table and use writetable.

More Answers (1)

Caglar on 2 Nov 2018
datestr is the function you are looking for. then, your date data is just a string.
for example
dpb on 3 Nov 2018
Edited: dpb on 3 Nov 2018
That's likely not the actual problem here; fprintf is now datetime-aware. Not sure exactly when was implemented; I just tested and R2014b wasn't yet, but was by R2016b.
Even with that change, OP would still have a problem owing to the two output variables in the argument list are each processed entirely in order, not sequentially through the pair and datestr(t) will produce a character string array that will get processed sequentially by Matlab internal column-major storage order and output something not exactly expected:
>> t=repmat(datetime('now'),3,1);x=randn(size(t)); % small sample data
>> fprintf('%s %f\n',datestr(t),x)
000333---NNNooovvv---222000111888 000999:::555333:::111000 0.929114
7.043032e-01 -0.119039
You can transpose the datestr output to get it in the proper order but the two arrays are still processed sequentially--
>> fprintf('%s %f\n',datestr(t).',x)
03-Nov-2018 09:53:1003-Nov-2018 09:53:1003-Nov-2018 09:53:10 9.291145e-01 0.704303
-1.190387e-01 >>
For composite outputs of compatible types, one can concatenate and transpose the output variables to write the vectorized form, but when the variable types are not commensurate that option isn't available --
>> [datestr(t) x]
ans =
3×21 char array
'03-Nov-2018 09:53:10 '
'03-Nov-2018 09:53:10 '
'03-Nov-2018 09:53:10 '
doesn't error, but it doesn't actually concatenate the double array onto the char array, either, so you don't get the output desired.
One could write something like
>> [datestr(t) num2str(x,' % 12.5f')]
ans =
3×28 char array
'03-Nov-2018 09:53:10 0.92911'
'03-Nov-2018 09:53:10 0.70430'
'03-Nov-2018 09:53:10-0.11904'
but then the [] ignores the spaces between the two variables in building the string.
So, all in all, the for loop is the simplest way by far to build a disparate-variable output record when desired to control the formatting.
ML really does need some higher-level output formatting routines that are capable of handling cell arrays and the various data types without having to write such low level code, granted.

Sign in to comment.


Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!