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

23 views (last 30 days)
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

Answers (2)

Siddharth Bhutiya
Siddharth Bhutiya on 22 Oct 2020
Edited: Siddharth Bhutiya on 22 Oct 2020
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 Comments
Steven Lord
Steven Lord on 10 Dec 2021
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.

Sign in to comment.


Eduardo Gil
Eduardo Gil on 16 Jun 2022
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.

Categories

Find more on Dates and Time in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!