フィルターのクリア

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

38 ビュー (過去 30 日間)
Magda Bielinski
Magda Bielinski 2016 年 1 月 25 日
編集済み: Joris Brouwer 2023 年 4 月 5 日
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 Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by