Effacer les filtres
Effacer les filtres

Calling C function generated in MATLAB coder

1 vue (au cours des 30 derniers jours)
Ife
Ife le 18 Août 2023
Commenté : Walter Roberson le 18 Août 2023
I've been writing code to (attempt to) efficiently reshape large h5 datasets. I'm curious if compiling it into a C-function makes it faster so I have used MATLAB coder to do that, as my experience in C is next to 0.
The original function is of the form
camData = read_streamLEDbttrLogic(eventStream,attributes,LEDtime_ms,macroRate)
eventStream is a column vector, attributes is a structure with attributes read from file, LEDtime_m and macroRate are both scalar doubles.
When loading in the compiled library and looking at the function with libfunctions I get the below output.
[emxArray_uint32_TPtr, struct0_TPtr, struct1_TPtr] read_streamLEDbttrLogic(emxArray_uint32_TPtr, struct0_TPtr, double, double, struct1_TPtr)
I'm just not terribly sure how to call it correctly in MATLAB. When I try to this happens
[a,b,c] = calllib('read_streamLEDbttrLogic','read_streamLEDbttrLogic',eventStream,att,LEDtime_ms,macroRate);
Error using calllib
No method with matching signature.
Am I totally off the mark here and need to recompile or am I just being an idiot with the syntax? The documentation around calling C functions is pretty lax and I cant figure out where it is going wrong.
  1 commentaire
Walter Roberson
Walter Roberson le 18 Août 2023
Note that MATLAB Coder does not generate code to operate on multiple cores, so it is common that the code generated by MATLAB Coder is overall slower than interpreted MATLAB.
A lot depends on what is being calculated. If you have non-vectorized code that is not doing matrix calculations (such as eig()) then the code generated by MATLAB Coder can certaintly be faster for that kind of code.

Connectez-vous pour commenter.

Réponses (1)

ProblemSolver
ProblemSolver le 18 Août 2023
The issue is that MATLAB Coder has generated C code that expects input arguments of specific types like emxArray_uint32_TPtr instead of just a normal MATLAB array. This is needed for the C code to know how to interpret the MATLAB data, but makes calling it a bit more complex.
Here is one way to call the compiled C function correctly from MATLAB:
  1. Convert the MATLAB arrays to the expected C types:
eventStream_c = libpointer('uint32Ptr',eventStream); % emxArray_uint32_TPtr
att_c = libstruct('struct0_TPtr', attributes); % struct0_TPtr
  1. Call the C function, passing the libpointer/libstruct objects:
[out1, out2, out3] = calllib('read_streamLEDbttrLogic', 'read_streamLEDbttrLogic', eventStream_c, att_c, LEDtime_ms, macroRate);
  1. Convert outputs back to MATLAB arrays:
eventStream_out = get(out1, 'Value');
attributes_out = get(out2,'Value');
This allows passing MATLAB data in a way the C code can interpret correctly. The Coder documentation has more details on interoperating with C code.
I hope this helps.

Catégories

En savoir plus sur MATLAB Coder dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by