Error: Grid values must be strictly increasing- interp2 function MATLAB Coder
Show older comments
Hi All,
This is my sample snippent. When I try to convert to C code using Matlab coder I am getting Index exceeds dimensions and Grid Values must be strictly increasing. However Dimensions error is fixed, can see here. Question seems big though, because I hard coded inputs for interp2 function. These two erros are haunting me from past week. I would appreciate if someone could shed light on this.
%codegen
function test()
CellModel=struct('SOCgrid',zeros(5,10),'Tgrid',zeros(5,10),'R0',zeros(5,10));
coder.varsize('SimCoreIn.R0_cell');
SimCoreIn=struct('R0_cell',zeros(102,3),'CellModel',CellModel,'SOC_cell',zeros(102,1),'T_cell',zeros(102,1));
SimCoreIn.CellModel.SOCgrid= [
0.8930 0.7860 0.6790 0.5720 0.4650 0.3580 0.2510 0.1430 0.0360 -0.0710
0.8930 0.7860 0.6790 0.5720 0.4650 0.3580 0.2510 0.1430 0.0360 -0.0710
0.8930 0.7860 0.6790 0.5720 0.4650 0.3580 0.2510 0.1430 0.0360 -0.0710
0.8930 0.7860 0.6790 0.5720 0.4650 0.3580 0.2510 0.1430 0.0360 -0.0710
0.8930 0.7860 0.6790 0.5720 0.4650 0.3580 0.2510 0.1430 0.0360 -0.0710]; %size 5 x 10
SimCoreIn.CellModel.Tgrid=[ 20 20 20 20 20 20 20 20 20 20
30 30 30 30 30 30 30 30 30 30
40 40 40 40 40 40 40 40 40 40
50 50 50 50 50 50 50 50 50 50
60 60 60 60 60 60 60 60 60 60]; %size 5 x 10
SimCoreIn.CellModel.R0 = [0.0120 0.0120 0.0130 0.0135 0.0135 0.0146 0.0147 0.0150 0.0160 0.0191
0.0120 0.0120 0.0129 0.0129 0.0129 0.0128 0.0130 0.0131 0.0136 0.0150
0.0125 0.0120 0.0119 0.0120 0.0119 0.0119 0.0120 0.0111 0.0114 0.0113
0.0116 0.0116 0.0116 0.0115 0.0115 0.0115 0.0115 0.0116 0.0129 0.0121
0.0112 0.0111 0.0111 0.0111 0.0111 0.0111 0.0111 0.0111 0.0124 0.0121];
%size 5 x 10
SimCoreIn.SOC_cell=[
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1]; %size 102 x 3
SimCoreIn.T_cell=[20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20
20 20 20]; %size 102 x 3
SimCoreIn.R0_cell=interp2(SimCoreIn.CellModel.SOCgrid,SimCoreIn.CellModel.Tgrid,SimCoreIn.CellModel.R0,SimCoreIn.SOC_cell,SimCoreIn.T_cell,'spline');
Here is the error image.

Thank You
2 Comments
Mukund Sankaran
on 8 Apr 2021
The documentation for interp2 (https://www.mathworks.com/help/matlab/ref/interp2.html) has a section on C/C++ code generation at the bottom which mentions this:
For best results, provide X and Y as vectors. The values in these vectors must be strictly monotonic and increasing
The X and Y in your example i.e. SimCoreIn.CellModel.SOCgrid and SimCoreIn.CellModel.Tgrid likely do not meet this criteria which is why you are seeing the error.
Naga Manoj Kumar Lakkoju
on 9 Apr 2021
Accepted Answer
More Answers (0)
Categories
Find more on MATLAB Coder in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!