MATLAB coder - selecting output type for entry point function

3 views (last 30 days)
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?

Answers (2)

Yoke Peng Leong
Yoke Peng Leong on 18 May 2018
You can consider creating the function such that the output oneByThreeArray is pass by reference:

Simone Pagliuca
Simone Pagliuca on 4 Dec 2022
Edited: Simone Pagliuca on 4 Dec 2022
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.

Categories

Find more on MATLAB Coder in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!