Is a two dimensional array in a structure supported by loadlibarary interface in MATLAB?

2 visualizzazioni (ultimi 30 giorni)
How do I pass a two dimensional array inside a structure to MATLAB while using LOADLIBRARY interface.

Risposte (1)

Murugavel Sivagnanam
Murugavel Sivagnanam il 12 Gen 2011
The ability to pass a two dimensional array in a structure is not available through LOADLIBRARY in MATLAB. Suppose following is the definition of the structure with p1 as the two dimensional array, p1.
typedef struct {
int p1[2][3];
int p2;
int p3;
} c_struct;
Following code shows the function foo which returns the pointer to a_struct created.
#include
#include
#include "mathworks_example.h"
c_struct a_struct = {{{1,2,3},{4,5,6}},7,8};
EXPORTED_FUNCTION c_struct* foo() {
return &a_struct;
}
If this structure is returned from the library to MATLAB. It would not be read correctly.
To workaround this issue, please generate a prototype file using the library and header. The protoype file for the above function definition would look like the following:
function [methodinfo,structs,enuminfo,ThunkLibName]=proto
%PROTO Create structures to define interfaces found in 'mathworks_example1'.
%This function was generated by loadlibrary.m parser version 1.1.8.25 on Mon Sep 20 17:13:34 2010
%perl options:'mathworks_example1.i -outfile=proto.m -chunkfile=mathworks_example_thunk_maci64.c'
ival={cell(1,0)};
% change 0 to the actual number of functions to preallocate the data.
structs=[];
enuminfo=[];
fcnNum=1;
fcns=struct('name',ival,'calltype',ival,'LHS',ival,'RHS',ival,'alias',ival,'thunkname', ival);
MfilePath=fileparts(mfilename('fullpath'));
ThunkLibName=fullfile(MfilePath,'mathworks_example_thunk_maci64');
% c_struct * foo ();
fcns.thunkname{fcnNum}='voidPtrThunk';fcns.name{fcnNum}='foo';
fcns.calltype{fcnNum}='Thunk';
fcns.LHS{fcnNum}='c_structPtr';
fcns.RHS{fcnNum}=[];
fcnNum=fcnNum+1;
structs.c_struct.members=struct('p1', 'int32#', 'p2', 'int32', 'p3', 'int32');
methodinfo=fcns;
The syntax of structure is interpreted by MATLAB by the following line in the above code:
structs.c_struct.members=struct('p1', 'int32#', 'p2', 'int32', 'p3', 'int32');
The parameter p1 is of the type 'int32' but the length of the array is missing. If p1 were a one dimensional array of length 5, the following would be the output in prototype file:
structs.c_struct.members=struct('p1', 'int32#5', 'p2', 'int32', 'p3', 'int32');
To workaround the limitation of two dimensional array in a structure please manually change the prototype file to assign the size of the two dimensional array, like the following:
structs.c_struct.members=struct('p1', 'int32#6', 'p2', 'int32', 'p3', 'int32');
% the number following the #, denotes the length of the array.
Now when we call the function using loadlibrary we would get a structure with p1 as a single dimensional array of length 6. We can reshape the array to a two dimensional array using the function RESHAPE.

Categorie

Scopri di più su Data Type Conversion in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by