How to get current date and time within a MATLAB Coder file

23 次查看(过去 30 天)
I am trying to code out a subset of functions from a MATLAB GUI into MEX files to speed up computation time. In the original MATLAB code, I use the clock function to get the current date and time and plan to return these values within a structure, i.e.
time = clock;
struct.year = time(1);
struct.month = time(2);
struct.day = time(3);
However, the clock function is not compatible with MATLAB coder. Is there any MATLAB function that gets the current date and time that IS compatible with MATLAB coder? I tried clock and now, but neither work. I want to output a time stamp from my code, so using coder.extrinsic to skip coding out the clock function and get the code to compile is not favorable.
If no functions are supported, is there any other way to get the current date and time and interpret them into a human-readable format?
Thank you in advance!
  1 个评论
Joris Brouwer
Joris Brouwer 2023-4-5
编辑:Joris Brouwer 2023-4-5
Just found out that this works (tested in R2021a and R2023a):
dNow = datetime('now');
YY = dNow.Year;
MM = dNow.Month;
DD = dNow.Day;

请先登录,再进行评论。

回答(2 个)

Andreas Jock
Andreas Jock 2021-1-7
This code workes for me with Matlab-Coder 2020A on a Jetson Xavier hardware:
dVec = ( datevec(datetime('now' )));
fprintf(fid, '%02d.%02d.%04d %02d:%02d:%02.3f', int16(dVec(3)), int16(dVec(2)), int16(dVec(1)), int16(dVec(4)), int16(dVec(5)), dVec(6) );
The output is like this :
07.01.2021 11:58:30.859
  2 个评论
Joris Brouwer
Joris Brouwer 2023-4-5
I have been using the same "datevec(datetime('now'))" construction in a R2021a for a while now and it worked very well for me. However now I am trying it in R2023a and suddenly its no longer "supported for code generation" inside the coder app. Documentation says it is supported though.
Joris Brouwer
Joris Brouwer 2023-4-5
Ah, I figured out that the solution to my conundrum was even more elegant, not sure since when this compiles but it does it great in R2023a:
dNow = datetime('now');
YY = dNow.Year;
MM = dNow.Month;
DD = dNow.Day;

请先登录,再进行评论。


AJ
AJ 2018-6-22
This code works for me, using Matlab R2016b, with Microsoft Visual Studio 2010:
function my_now = now_coder
%now_coder - Implementation of 'now' function using Coder
% For stand-alone code generation (for testing this function):
%{
codegen('-config:mex','-report','now_coder.m')
%}
if coder.target('MATLAB')
my_now = now;
else
% The _time64() function returns the UTC time in seconds since midnight Jan 1, 1970
timer1 = coder.opaque('__time64_t','0','HeaderFile','time.h');
coder.ceval('_time64',coder.ref(timer1));
% Use memcpy() to get to the value from the "C" to the "Matlab" domain
T1 = uint64(0);
coder.ceval('memcpy',coder.ref(T1),coder.ref(timer1),int32(8));
% Not sure if this is needed:
coder.ceval('_tzset'); % assign values to three global variables: _daylight, _timezone, and _tzname.
timezone = coder.opaque('long','0');
coder.ceval('_get_timezone',coder.ref(timezone));
T2 = uint32(0);
% Use memcpy() to get to the value from the "C" to the "Matlab" domain
coder.ceval('memcpy',coder.ref(T2),coder.ref(timezone),int32(4));
%fprintf('TIMER=%g\n', double(T1));
%fprintf('Timezone=%u\n', T2);
epoch = 719529; % datenum([1970 1 1 0 0 0])
my_now = epoch + double(T1-uint64(T2))/(24*3600);
end

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by