How to use coder.ceval to call a void type C++ function

조회 수: 5 (최근 30일)
Erjian
Erjian 2021년 4월 2일
댓글: Darshan Ramakant Bhat 2021년 4월 5일
Hi,
I am trying to call an external C++ function, which is a void type function. Here is the code I wrote as an example
function [Y] = example(a,b)
coder.updateBuildInfo('addSourceFiles','cpp_fun.cpp');
coder.cinclude('cpp_fun.h');
coder.ceval('cpp_fun', a, b, Y);
end
The C++ function is defined as:
void cpp_fun(int a, float *b, float *Y);
Here int a, float *b are input of the function and *Y is suppose to be the output of the C++ function.
But when I use the Matlab Coder to convert the matlab function above, I get the following error message: "Variable 'Y' is not fully defined on some execution paths."
Shoud I use something like
Y = coder.opaque(type)
in the matlab function to define Y? If yes, what type should I use for Y?
Any help is greatly appreciated!

답변 (1개)

Darshan Ramakant Bhat
Darshan Ramakant Bhat 2021년 4월 2일
Try defining the variable 'y' in MATLAB function then try to use coder.wref()
Y = 0;
coder.ceval('cpp_fun', a, b, coder.wref(Y));
Similarly you may need to use coder.ref() for the second input 'b' since it is a pointer.
Hope this will help you.
  댓글 수: 2
Erjian
Erjian 2021년 4월 2일
Hi Darshan,
Thanks fo your help! While I was trying your suggestions and I ran into other problems. I simplifed my C++ function to better understand how this should work. Here is the modified matlab function:
function [Y] = example(a,b)
if coder.target('MATLAB')
% Executing in MATLAB, call function example
Y = a+b;
else
coder.updateBuildInfo('addSourceFiles','cpp_fun.cpp');
coder.cinclude('cpp_fun.h');
Y = 0;
coder.ceval('cpp_fun', a, b, coder.wref(Y));
end
end
And here is the simplified C++ code:
void cpp_fun(int a, int b, float y)
{
y = (float)a + (float)b;
}
And here is the screeshot of the error I got in the Matlab Coder Report Viewer:
How should I handle this data type inconsistency problem?
Darshan Ramakant Bhat
Darshan Ramakant Bhat 2021년 4월 5일
You need to use proper types at the interface. From the error I can see that you are passing "y" as pointer but in C++ code you are taking by value. This is giving the error. Also type of "y" is real_T, you need to make it as float.
I have modified your example and made it work. Please check the attached file and run doit.m file.
Hope this will help you.

댓글을 달려면 로그인하십시오.

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by