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

3 ビュー (過去 30 日間)
Eugene K
Eugene K 2024 年 4 月 9 日
コメント済み: Eugene K 2024 年 4 月 10 日
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 Center および File ExchangeMATLAB Coder についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by