Function 'datenum' not supported for code generation. Function 'MATLAB Function'

8 ビュー (過去 30 日間)
Seema Borase
Seema Borase 2020 年 6 月 20 日
回答済み: Eduardo Gil 2022 年 6 月 16 日
The following picture is a part of the Simulink model and I use the MATLAB Fcn (Library function) to call the datenum function.
functiontime = funcPlotGraph(xAxis, yAxis, timeperiod1, timeperiod2)
legendsCol = {};
dim = 2;
% Convert long tick time to DateTime
time = datetime(datestr(xAxis/86400000 + datenum(1970,1,1)));
% plot graph between time vs yAxis
plot(xAxis, yAxis);
legendsCol{end+1} = 'Actual';
hold on
if ~isequal(timeperiod1, '0')
% average
movingAve = movmean(yAxis, timeperiod1);
plot(time, movingAve);
legendsCol{end+1} = 'Avg';
end
if ~isequal(timeperiod2, '0')
% average
movingAve20 = movmean(yAxis,timeperiod2);
plot(time, movingAve20);
legendsCol{end+1} = 'Avg 2';
I'm getting the following error while compiling the model:
Function 'datenum' not supported for code generation. Function 'MATLAB Function' (#88.184.201), line 5, column 43: "datenum(1970,1,1)" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Simulink does not have enough information to determine output sizes for this block. If you think the errors below are inaccurate, try specifying types for the block inputs and/or sizes for the block outputs.
Component:MATLAB Function | Category:Coder error
Undefined function or variable 'time'. The first assignment to a local variable determines its class. Function 'MATLAB Function' (#88.409.413), line 15, column 14: "time" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Undefined function or variable 'time'. The first assignment to a local variable determines its class. Function 'MATLAB Function' (#88.558.562), line 22, column 8: "time" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Errors occurred during parsing of MATLAB function 'TestMatlabModelOld/MATLAB Function'
Component:MATLAB Function | Category:Coder error
Simulink cannot determine sizes and/or types of the outputs for block 'TestMatlabModelOld/MATLAB Function' due to errors in the block body, or limitations of the underlying analysis. The errors might be inaccurate. Fix the indicated errors, or explicitly specify sizes and/or types for all block outputs.
Component:MATLAB Function | Category:Coder error
Simulink cannot determine sizes and/or types of the outputs for block 'TestMatlabModelOld/MATLAB Function' due to errors in the block body, or limitations of the underlying analysis. The errors might be inaccurate. Fix the indicated errors, or explicitly specify sizes and/or types for all block outputs.
Component:Simulink | Category:Model error

回答 (2 件)

Siddharth Bhutiya
Siddharth Bhutiya 2020 年 10 月 22 日
編集済み: Siddharth Bhutiya 2020 年 10 月 22 日
As the error indicates, datenum is not supported for code generation. However, datetime does support code generation and it is recommended to use datetime over datenum, even when not doing code generation.
Is there a specific reason you are trying to use datenum over datetime ?
  2 件のコメント
Piotr Kolodziej
Piotr Kolodziej 2021 年 12 月 10 日
How can I convert datetime result to "Serial date number" with code generation?
Steven Lord
Steven Lord 2021 年 12 月 10 日
Let's get a sample date number.
n = datetime('now') % Use for comparison later
n = datetime
10-Dec-2021 14:05:23
d = datenum(n)
d = 7.3850e+05
Now to convert d back into a datetime like n, call datetime with the 'ConvertFrom' name-value pair argument.
n2 = datetime(d, 'ConvertFrom', 'datenum')
n2 = datetime
10-Dec-2021 14:05:23
I believe this syntax is supported for code generation.

サインインしてコメントする。


Eduardo Gil
Eduardo Gil 2022 年 6 月 16 日
Well, I tried datetime(d,'ConvertFrom','datenum') for an Excel addin - it didnt work (the compiled code returns me an error, even though in Matlab the function works fine). Problem is in Excel a date is a number - and there is a different date convention in Excel vis a vis Matlab. So this function would be a good way to make the conversion. Any thoughts here? I had to google to the end of the Internet to find this post - which seems very much related to the error I am getting.

カテゴリ

Help Center および File ExchangeApp Building についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by