griddedInterpolant bug with Matlab Coder in R2022b

Hi All,
I am trying to make use MATLAB Coder (version 5.5) for griddedInterpolant, But I am getting the following errors
"??? Sample points must be 1-dimensional for code generation."
clearvars; clc; close all;
x = -5:0.8:5;
y = -5:0.8:5;
[xg,yg] = ndgrid(x,y);
vg = sin(xg.^2 + yg.^2) ./ (xg.^2 + yg.^2);
surf(xg,yg,vg); hold on
xq = -4;
yq = 1.2;
vq = griddedInterpolant_test(x,y,xq,yq);
plot3(xq,yq,vq,'r*');
codegen -report -config:lib griddedInterpolant_test.m -args {-5:0.8:5, -5:0.8:5, -4, 1.2}
In my original code, I also get the following error even though xg, yz, zg matrices in griddedInterpolant(xg,yg,zg) are all having a fixed (m x n) size and when the interpolant is evaluated at a single xq, yq.
"??? Unable to determine varargin/varargout index due to failed loop unrolling."
Does it mean that MATLAB Coder is not compatible with 2-dimensional interpolation when griddedInterpolant is used ?
Please write to me if you need any additional details

1 comentario

vg is generated inside the griddedInterpolant_test function based on the same xg and yg. It is additionally generated in the script only for plotting the surface (because visualizing the problem makes it easy to understand)

Iniciar sesión para comentar.

 Respuesta aceptada

Thomas Best
Thomas Best el 8 de Ag. de 2023
Editada: Thomas Best el 8 de Ag. de 2023
I had the same error - updating to R2023a fixed it for anyone else running into the same problem.

Más respuestas (1)

Steven Lord
Steven Lord el 10 de Nov. de 2022
The first error you received is not a bug. The fact that griddedInterpolant requires that the sample points be vectors rather than matrices or N-dimensional arrays when used in code generation with MATLAB Coder is a documented limitation. See the "C/C++ Code Generation" item in the Extended Capabilities section of the documentation page to which I linked above.
I am not certain of the reason you received the second error message you quoted. From the fact that you said you received this with "my original code" I assume that you ran a different code than the one you posted to receive that error. Without seeing that code I'm not sure if it would be possible to determine the cause of this error.

16 comentarios

Hi Steven,
Thanks for your reply.
Can you perhaps let me know (with a small example may be) how to specify the sample points for a 2D interpolation in vectors and create a griddedInterpolant object? You can see from my attached example that the sample points here is the vg (which is generated from xg and yg).
I will not be able share my orignal code in this forum. But if I can get the attached example here to be working - then I can implement the same architecture to my original code.
My first thought would be to try to reshape the arrays into vectors, call griddedInterpolant with those vectors of query points, and reshape them back into arrays. But I have not tried to generate code from the code you posted with that modification so I'm not 100% sure that this will not cause difficulties for MATLAB Coder.
You may wish to send your original code to Technical Support directly using the Contact Support link under the Get Support heading at the end of this page and work with the Support staff to determine the best approach for generating code from it. There is no guarantee that suggestions we make for your simplified code posted in this discussion will generalize to your larger and presumably more complicated code.
Hi Steven,
I did try to reshape the input arrays into vectors and supplied them as per F = griddedInterpolant(gridVecs,V). The example it points to also has the same input data structure like my case here.
Everything works well within MATLAB - but the problem is with the code generation. When I supply the inputs as grid vectors, I now get the following error during code generation.
??? Interpolation requires at least two sample points for each grid dimension.
I can see the description from sample values that we need an extra dimension compared to the grid of sample points - But I just dont know how to generate them and the example also doesnt talk about it.
Can you please give me an example of how the sample values V needs to be generated for any example as per the definition (in second bullet) given in here.
%% 2D
clearvars; clc; close all;
xg = (-5:0.8:5)';
yg = -3:1:3;
vg = sin(xg.^2 + yg.^2) ./ (xg.^2 + yg.^2);
[X,Y] = ndgrid(xg,yg);
surf(X,Y,vg); hold on
xq = -4;
yq = 1.2;
vq = griddedInterpolant_2d_upd(xg,yg,xq,yq);
plot3(xq,yq,vq,'r*');
codegen -report -config:lib griddedInterpolant_2d_upd.m -args {-5:0.8:5, -5:0.8:5, -4, 1.2}
Unable to run the 'fevalJSON' function because it calls the 'codegen' function, which is not supported for this product offering.
Please note that there is no problem with code generation for 1d interpolation using griddedInterpolant -The problems happen only in the 2d interpolation using griddedInterpolant.
Hi Bruno, Thanks - But as you can see - I already posted the same.
My question is not about how to get it working within MATLAB. But about how to write it in such a way that it is compatible with MATLAB Coder for code generation.
Hi Bruno,
I have the same logic in the file I have attached in my example here.
But when I use your function, I get a different error in Coder which is
"??? Sample points must be 1-dimensional for code generation.". So it just comes to this...even though the x is a column vector and y is a row vector (when passed into the function), the following piece of code has an impact in Code Generation (but not in MATLAB)
x = x(:);
y = y(:).';
Without the above two lines of code, the error is same as my earlier comment here.
I have a feeling that there is an issue with Coder (not MATLAB) and only someone who has Coder 5.5 installed can be able to help a bit further.
If any of the staff has access to Matlab r2022b ( and Coder 5.5) - then it would be very easy for them to reproduce the issue I am highlighting (with the same code and file I have shared in my comment) and understand what I am saying here and possibly suggest a solution to overcome this.
If you want an official Technical Support answer, please contact Technical Support directly using the Contact Support link under the Get Support heading at the end of this page.
Hi Bruno,
I dont think we understand each other.
1) The coder is not able to compile at all and it shows errors.
2) All the code and how the function is called is already explained in both of my comments with code snippets. Please just download the attached file and simply run the code from my comment
3) Please just see my example
4) Can you perhaps show me a coder compatible code example (which does 2d interpolation using the griddedInterpolant) ?
If you do not have coder 5.5 - You will not be able to understand the point I am saying here...
Steven, Its not about technical support - Its about the right documentation and the willingness to help the community members.
I see that MATLAB staff is pretty reluctant in accepting a bug (in coder) and giving lame reasons like they dont have the (coder) toolbox available to check it precisely.
If there is no bug - why dont the support staff point me to an example which explains the problem I am highlighting here (or create an example? Its not 100s of lines of code - Its definitely not more than 10 lines).
Hi Bruno,
Initially you claimed that griddedInterpolant is not supported for code generation at all and then I proved you wrong by pointing you to MATLAB's own release notes. I have already given all the required code to reporduce the problem I am highlighting - But if you do not have coder 5.5 - you will never be able to see the issue I was trying to say. I never said your help bothered me. What bothers me is that, 1) the documentation is not clear and 2) no one from support staff is willing to accept either there is a bug in coder or prove it otherwise with an example
If you contact the Technical Support staff you can speak to people with more experience with MATLAB Coder than I have. I don't know whether or not the second error message you posted is the result of a bug.
Torsten
Torsten el 10 de Nov. de 2022
Most of the people in this forum do not belong to Mathworks staff. I can understand that you are frustrated, but I wonder why you deposit your frustration here.
Do as Steven Lord says: Contact the Technical Support. The people there get their money to help you.
Hi Steven, Torsten Thanks for your replies. I have created a service request with Technical Support and the case number is 05848451. I hope they get back within a day as promised.
I dont think I was frustrated nor deposited here and I wonder why it has been perceived in that way especially by Bruno. I was only speaking straight with my examples and asking questions in a straight forward way. Its mathematical software forum right? 2+2=4 and never 3.9 or 4.1.
But lets not sidetrack ourselves from the main issue that I highlighted here. I welcome anyone who has coder 5.5 to try my examples and get an understanding of the problem I am highlighting here and possibly suggest a solution
"If there is no bug - why dont the support staff point me to an example which explains the problem"
Perhaps because there are no support staff reading this Question? Bruno and Torsten do not work for Mathworks at all, and Steven Lord is not support staff. Steven Lord is here as a volunteer — an activity that he does not get paid for. I do not recall quite what his job is, but I recall that he is not support staff, and that he does not use the compiler tools much in his job.
Hi All,
When I posted this question (along with reproduceable code) - I was pretty clear that the question is related to Coder 5.5 (and not with main MATLAB). So Isn't it fair to expect answers from the community who has worked/experience in Coder 5.5?
I see like we are diluting the core of the problem by side tracking. If you are commenting in the Answers section for a technical query - then I expect the answers to be technical and leading to a solution for the problem I have posted. In short I expect bulls-eye kind of technical answers (from someone who has experience in Coder 5.5) rather than beating around the bush.
If your comment is not towards acknowledging/solving the question I have posted/asked - Then please don't spam this Answers section by commenting general stuff.
For those who are interested, Here is the reply (for case number 05848451) from MATLAB Technical support on this.
"griddedInterpolant supports only 1D inputs for code generation as of R2022b, we'll enable support for 2D and nD inputs only in the next release.
Enabling 2D inputs requires significant changes, that can't be implemented in R2022b"
And they have set the current status of my case to Bug/Enhancement Submitted
If you are commenting in the Answers section for a technical query - then I expect the answers to be technical and leading to a solution for the problem I have posted. In short I expect bulls-eye kind of technical answers
Well, that certainly might be what you expect but it is not what this resource was created for.
This MATLAB Answers resource was created for members of the public to ask other members of the public for assistance with Mathworks related topics. It is not an official support forum from Mathworks, and any Mathworks employees who respond here do so as volunteers (with the exception of people running the facility itself.)
Experts in a specialized topic might not happen to see any particular question -- there are between 100 and 200 new Questions per day, and discussion on more than 200 different topics per day even during slow times -- and topic experts are often busy doing other thing.
It is normal and expected here, an inherent part of why the facility was set up, that responses from volunteers might be along the lines of "I don't know for sure, but let us work together to see if we can come up with some solution together."
It also happens a lot of the time that people asking questions do not explain well, or that people turn out to be asking about something different than what they actually need, so it is common that the volunteers end up asking a bunch of questions in order to figure out what is really needed and to figure out what is happening (and try to figure out why, and what the work-arounds might be.)
A lot of the people who ask questions do not have English as their native language, and a fair number of the volunteers do not have English as their native language, so there are also translation issues, and differences in how terms are to be intepreted.
The purpose of the facility is for people doing their best to help other people, not questions to only be responded to by topic specialists who already know the answer or can definitely find out (such as by having access to internal source code.)

Iniciar sesión para comentar.

Categorías

Productos

Versión

R2022b

Preguntada:

el 10 de Nov. de 2022

Editada:

el 8 de Ag. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by