I have converted a function which takes a NxN matrix as input and gives a NxN matrix output from matlab to C, using the MatlabCoder. It gave me the function which has two parameters namely
void func(const emxArray_real_T *x, emxArray_real_T *y)
I get that x is the input to the function and i can get the output of the function from y. The problem is i have a array in float[][] and i wish to give this data as an input to the func, which only takes emxArray_real_T *x as an input.
Any ideas on how to convert this float[][] data to emxArray_real_T *x
emxArray_real_T has this structure
struct emxArray_real_T
{
real_T *data;
int32_T *size;
int32_T allocatedSize;
int32_T numDimensions;
boolean_T canFreeData;
};
I found this later that we can create any data into the emxArray_real_T * using these following steps...
Step 1: Lets say you have a array with float or double or anything else... it is of 2 dimensions.. it can be of any dimensions... it can be 3.. it can be of 4... now first step is to declare
where x will be your input and you will get the output in y...
now till this time we have only declared the variables and not set any dimensions or any size in them...
Step 2: In the same file as your function that you want to call there should be these functions.. if not then you must declare them....
now you can initialize your variables "x,y" as following
here 2 is the dimension of the matrix that we want as input or output..
Step 3: In my case the dimension of x is 2 so what i will do is like this Say my input is n*n matrix so i will do this
and similarly for y
if you have 3 or 4 dimension then you can put more like x->size[2]=that size.. and so on..
Step 4: Now we have ensure that the program allots the specified memory to these variables so for that there should be a function like below.. if not then your must declare it...
for this there should be struct names emxArray__common like below if not then do declare it like below
Step 5: then do like this..
now both these varibles have the specified n*n memory in them. now the input data will be saved in the data of x... that will be saved in it row wise..
if you get what i mean by it. now you can pass the x and y in the function that you want to call and then the data returned will be in y and it will be row-wise too.. so read it carefully and ther you have it... you have executed the matlab function in c....
Basically you need to have the
*data
member ofemxArray
point to thefloat
array. In C,float
data is stored assingle
precision. Update the other members ofemxArray
accordingly.You may want to checkout the MathWorks documentation on the C Code Interface for Arrays
This answer contains exact same scenario implemented in C# for
double
data-type.