Variable types in coder generated C - use in existing C codes

8 visualizzazioni (ultimi 30 giorni)
Hi,
I want to use Coder to translate some functions into C, then I want to use the generated code in other native C programs. I have some questions regarding variable types (particularly cell arrays)
Suppose I have the following 'do nothing' code...
clear
names = {'contrast1','contrast2','contrast3'};
data = {[1 2 3],[4 5 6],[7 8 9]};
saveStruct(names,data);
where the function 'saveStruct' is just... (i.e. does nothing - I just want to see the codes..)
function saveStruct(names,data)
a = 1;
end
Now, if I run saveStruct through coder with dynamic array sizing, I get.....
#include "rt_nonfinite.h"
#include "saveStruct.h"
/* Function Definitions */
void saveStruct(const emxArray_cell_wrap_0 *names, const emxArray_cell_wrap_1
*data)
{
(void)names;
(void)data;
}
It looks like the emxArray_cell_wrap is some sort of wrapper that TMW has developed to handle dynamic sized cell arrays. I have three main questions here...
1. Suppose I wanted to call this function from native C. What would the incoming variable types be? Would I need to import this wrapper into the calling code and create these 'cell arrays' manually? If so, what would the 'main' funtion look like in this case? (is there Doc??)
2. Ditto, suppose I want to call another function from here. How would that look 'downstream'?
3. If I wanted to save/load names and data to/from a file from within saveStruct? How would that be done?
Finally, does anyone know of any blogs/examples etc that show an example of the use of Coder generated code in an Eclipse-CDT project?
Many Thanks,
Arwel

Risposta accettata

Shivang Menon
Shivang Menon il 24 Mar 2017
In generated code, MATLAB represents dynamically allocated data as a structure type called emxArray. This is the structure that MATLAB forces on variable sized arrays. So if you have a variable sized array as inputs, then MATLAB Coder will treat those inputs as emxArrays. That is what is happening in your code.
However, when you use MATLAB Coder to generate code that uses variable-sized data, the coder exports a set of utility functions that you can use to interact with emxArrays. In particular, the utility function 'emxCreateWapperND_<baseTypeName>' is useful for interfacing with externally defined variable-sized data. This function creates a new N-dimensional emxArray, but does not allocate it on the heap. Instead it uses memory provided by the user.
1] To see, how to create and use the generated C function, you can take a look at the example main that is created. By default, MATLAB Coder generates a main. If it is not generated, check the settings of MATLAB Coder and search for the parameter 'Generate example main' and change it accordingly. You would need to do something similar to what is being done in main, in the external code from which you want to call the generated C function. You would need to create emxArrays and pass them to the function, like in the generated main.
Refer to the following link for emxArray utility functions:
2] I created a function similar to yours, and specified names and data to be variable size array of size 1xn with n<1000 and containing double data. This created an emxArray struct as shown below:
struct emxArray_real_T
{
double *data;
int *size;
int allocatedSize;
int numDimensions;
boolean_T canFreeData;
};
The emxArray utilities provide a way to create variable size data, and emxArrays store this data along with other parameters such as size. To call downstream functions, you can extract the data from the emxArray struct object created and pass it.
Regarding your last question related to Eclipse, you can relocate generated code to an external environment using packNGo. Refer to following examples:
Hope this helps.

Più risposte (1)

Sri Murali
Sri Murali il 14 Feb 2023
how to solve this equation in MATLAB

Categorie

Scopri di più su MATLAB Coder in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by