need to timestamp to ms resolution AND be able to codegen it to C++

3 次查看(过去 30 天)
datetime command is doing what i need (provides time resolution to ms) BUT its not allowed by codegen. Is there equivalent command that does codegen? datetime('now','TimeZone','local','Format','d-MMM-y HH:mm:ss:ssssss Z'); Alternatively, datetime('now') codegens BUT need resolution to ms.

采纳的回答

Ramtej
Ramtej 2024-4-10
Hi
While you can't directly use the full formatting and timezone features of the "datetime" function in a codegen context, you can still capture high-resolution time. One approach is to use basic "datetime" functionality that is supported by codegen and then manually manipulate or format the output as needed.
Here's the workaround:
  • Create your own datetime function which outputs the milli seconds by manipulating the original "datetime" output
function [year , month, day, hour, minute, second,ms] = myDatetime()
currentTime = datetime('now');
year = currentTime.Year;
month = currentTime.Month;
day = currentTime.Day;
hour = currentTime.Hour;
minute = currentTime.Minute;
second = currentTime.Second;
integ=floor(second);
ms=floor((second-integ)*1000); % Note sometimes you may loose precision here when number of decimals are greater the 4
end
  • Replace the builit-in "datetime" function with your function "myDatetime" in your code and regenerate your C/C++ code.
  2 个评论
Eugene K
Eugene K 2024-4-10
Thank you. This converts to C++ but there is a resolution issue. When event happens I put a timestamp. In Matlab i see 6 numbers after decimal. But when codegen runs it there are only 3 digits after decimal. I realized hat i do need more than ms resolution. Is there a way to increase both MAtlab and C++ resolution?
In Matlab:
Date: 2024- 4-10 Time: 8:46:54.013099
Date: 2024- 4-10 Time: 8:46:54.024811
Date: 2024- 4-10 Time: 8:46:54.036219
In C++
Date: 2024- 4-10 Time: 8:48:43.122000
Date: 2024- 4-10 Time: 8:48:43.127000
Date: 2024- 4-10 Time: 8:48:43.132000
Eugene K
Eugene K 2024-4-10
just fyi, i am doing this...
T = datetime('now');
fprintf('Date: %4.0f-%2.0f-%2.0f Time: %2.0f:%2.0f:%9.6f ', T.Year, T.Month, T.Day, T.Hour, T.Minute, T.Second);
fprintf('\n');

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB Coder 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by