MATLAB coder - selecting output type for entry point function

조회 수: 4 (최근 30일)
Daniel Murphy
Daniel Murphy 2018년 5월 10일
편집: Simone Pagliuca 2022년 12월 4일
I'm trying to use matlab coder to create C++ code, compile into a DLL and use this inside a C# project. I have a single entry point function that returns a 1x3 array in matlab, i.e.
function oneByThreeArray = exampleFunction(input1, input2, input3,...){...}
When coder is run, the default behaviour is to convert this entry point to
void exampleFunction(const double input1, const double input2, const double input3,..., double oneByThreeArray[3])
So my two questions are
  • is it possible to set the C++ output type (rather than void) of my entry point function so that it returns a value for oneByThreeArray (e.g. std::array) before the code is generated by the coder?
  • if not, how should this C++ code be called from within C#? If oneByThreeVector is initialised just before the function call, will it be filled with the desired output values afterwards?

답변 (2개)

Yoke Peng Leong
Yoke Peng Leong 2018년 5월 18일
You can consider creating the function such that the output oneByThreeArray is pass by reference:

Simone Pagliuca
Simone Pagliuca 2022년 12월 4일
편집: Simone Pagliuca 2022년 12월 4일
I know this is old but since I encountered the same problem, I'll post the solution anyway.
In my testing using an array creates some problems (I'll edit if i find a solution), but you can pull it off with multiple values:
So instead of
function array = myfunction(arg1, arg2)
you write it like
function [value1, value2, value3] = myfunction(arg1, arg2)
where the 3 values are you arrays elements.
Then, you generate the DLL.
Then, C#:
First of all, import the dll:
[DllImport("myfunction.dll",
CallingConvention = CallingConvention.Cdecl)]
public static extern void myfunction(Double arg1, Double arg2, out Double value1, out Double value2, out Double value3);
note that the function is of type void and the outputs are set as arguments with the prefix out.
Then to use it:
double value1;
double value2;
double value3;
sg_geometry(arg1, arg2, arg3, out value1, out value2, out value3);
So now you have your results in the variables value1, value2, value3.

카테고리

Help CenterFile Exchange에서 MATLAB Coder에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by