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

조회 수: 24 (최근 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 CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by