Codegen fails with "wrong number of input arguments" while function calls without error

5 Ansichten (letzte 30 Tage)
I'm trying to generate a mex file for the following function:
function matInt = testfun(a,b,M)
matfun = @(x) squeeze(interpn([-1 0 1],[-1,0,1],M,x(1),x(2)));
matInt = integral(@(s) matfun(a*s+(1-s)*b), 0, 1, 'ArrayValued',true);
end
using the following script:
clear; close all;
%% Test arguments
a = [.5;.3];
b = [-.2;-1];
M = randn(3,3,5,5);
%% Run function
Mint = testfun(a,b,M); % okay
%% Codegen
% error:
codegen testfun -args {a,b,M}
Running the function (so computing Mint) goes fine without any errors. But generating a mex file using the same input arguments (in the last line) throws the error:
??? Wrong number of input arguments.
Error in ==> testfun Line: 2 Column: 23
Code generation failed: View Error Report
I'm not exactly what goes wrong there, as I do not see how I do not give the correct number of input arguments.

Akzeptierte Antwort

Mike Hosea
Mike Hosea am 1 Nov. 2022
Bearbeitet: Mike Hosea am 2 Nov. 2022
I can't find where this behavior of INTERPN is documented. To the contrary, we have:
>> help interpn
interpn N-D interpolation (table lookup).
Vq = interpn(X1,X2,X3,...,V,X1q,X2q,X3q,...) interpolates to find Vq,
the values of the underlying N-D function V at the query points in
arrays X1q,X2q,X3q,etc. For an N-D V, interpn should be called with
2*N+1 arguments. [snip]
I mean, the behavior is obviously useful, but the codegen version is only trying to support documented INTERPN behavior. You can rewrite your example to avoid relying on this behavior by using a nested function instead of an anonymous function:
function matInt = testfun(a,b,M)
% matfun = @(x) squeeze(interpn([-1 0 1],[-1,0,1],M,x(1),x(2)));
function Mq = matfun(x)
mout = size(M,3);
nout = size(M,4);
Mq = zeros(mout,nout,'like',M);
for j = 1:nout
for i = 1:mout
Mq(i,j) = interpn([-1 0 1],[-1,0,1],M(:,:,i,j),x(1),x(2));
end
end
end
matInt = integral(@(s) matfun(a*s+(1-s)*b), 0, 1, 'ArrayValued',true);
end
Seems like there should be another, more clever way, but it's not coming to mind at the moment.

Weitere Antworten (0)

Kategorien

Mehr zu Code Verification finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by