Dereferencing Pointer of Varying Size Struct Array (emxArray_struct0_TPtr) in MATLAB
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I want to call `C`-function in `MATLAB` generated shared library. Output of the function is a varying size struct array thus `MATLAB` puts it in `emxArray` form. Moreover, `MATLAB-Coder` make this output as pointer of the `emxArray` and pass it as an input argument and the trouble shows up here.
I can load the shared library and can call the `C`-function in `MATLAB`, but I can not extract the all output data from this `C`-function call.
Here, the example, generates the stiuation:
- I have the following .m file (named `trialFun.m`), targeted for code generation in `MATLAB-Coder`.
function output = trialFun(input) %#codegen
assert( isa(input, 'int32') && all( size(input) == 1 ) );
coder.varsize('output');
defaultStruct = struct('a', 0, ...
'b', 0, ...
'c', 0);
output = repmat(defaultStruct, 0, 1);
for ii = 1:input
defaultStruct.a = double(ii);
defaultStruct.b = defaultStruct.a;
defaultStruct.c = defaultStruct.a;
output = [output; defaultStruct];
end
end
- I build this using the following build script (default settings from the `MATLAB-Coder` app)
cfg = coder.config('dll');
codegen -config cfg trialFun;
- The resulting generated `C` code from this build process looks like this:
/* Include Files */
#include "rt_nonfinite.h"
#include "trialFun.h"
#include "trialFun_emxutil.h"
/* Function Definitions */
/*
* Arguments : int input
* emxArray_struct0_T *output
* Return Type : void
*/
void trialFun(int input, emxArray_struct0_T *output)
{
int i0;
int ii;
struct0_T defaultStruct;
int xs;
i0 = output->size[0];
output->size[0] = 0;
emxEnsureCapacity((emxArray__common *)output, i0, (int)sizeof(struct0_T));
for (ii = 1; ii <= input; ii++) {
defaultStruct.a = ii;
defaultStruct.b = ii;
defaultStruct.c = ii;
xs = output->size[0];
i0 = output->size[0];
output->size[0] = xs + 1;
emxEnsureCapacity((emxArray__common *)output, i0, (int)sizeof(struct0_T));
output->data[xs] = defaultStruct;
}
}
/*
* File trailer for trialFun.c
*
* [EOF]
*/
- Finally, I call the `C`-function in `MATLAB` generated shared library in this script:
[notfound, warnings] = loadlibrary('.\codegen\dll\trialFun\trialFun.dll', ...
'.\codegen\dll\trialFun\trialFun.h', ...
'mfilename', 'PROTOFILE_TRIALFUN');
defaultStruct = struct('a', 0, ...
'b', 0, ...
'c', 0);
struct_of_emxArray_struct0_T.data = libpointer('struct0_TPtr', defaultStruct);
struct_of_emxArray_struct0_T.size = libpointer('int32Ptr', int32(0));
struct_of_emxArray_struct0_T.allocatedSize = int32(0);
struct_of_emxArray_struct0_T.numDimensions = int32(0);
struct_of_emxArray_struct0_T.canFreeData = uint8(0);
pointer_of_struct_of_emxArray_struct0_T = libpointer('emxArray_struct0_TPtr', struct_of_emxArray_struct0_T);
input = int32(5);
calllib('trialFun', ...
'trialFun', ...
input, ...
pointer_of_struct_of_emxArray_struct0_T);
clear all;
unloadlibrary('trialFun');
where `pointer_of_struct_of_emxArray_struct0_T` is the output of the `C`-function and when I write the following code to command window:
>> pointer_of_struct_of_emxArray_struct0_T.Value.size
ans =
5
I get what I expected, also I can only extract the first element of the output struct array correctly typing:
>> pointer_of_struct_of_emxArray_struct0_T.Value.data
ans =
a: 1
b: 1
c: 1
In order to get all elements of the output struct array, I tried the following method to unable to apply pointer arithmetic:
>> x = libpointer('struct0_TPtr', pointer_of_struct_of_emxArray_struct0_T.Value.data);
>> x = x + 1;
>> x.Value
ans =
a: -3.785844157964855e-270
b: 2.848596587136785e-315
c: 0
Bu I get only trash data.
*Question:* How can I extract the other (second and so on) elements of the output struct array?
0 Kommentare
Antworten (0)
Siehe auch
Kategorien
Mehr zu MATLAB Coder finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!