Borrar filtros
Borrar filtros

Problem in using mxSetPr with USHORT

1 visualización (últimos 30 días)
Wang Yifan
Wang Yifan el 17 de Feb. de 2022
Comentada: Wang Yifan el 22 de Feb. de 2022
I create an array in C, and the element of the array is USHORT, counting for 2 Bytes. Now, I wanna transfer this array to MATLAB workspace, using MATLAB-C API. The current method I used is to copy the value of each elements one by one in a for-loop. It is so inefficient. Therefore, I want to use pointers. However, I found the function mxSetPr demands the input type is double*. USHORT is determined by the hardware I used. Is there any similar function which can accomplish the address transformation from C to MATLAB workspace, and is valid for USHORT.

Respuesta aceptada

Jan
Jan el 17 de Feb. de 2022
Either use memcpy instead of copying the data in a loop.
Or use mxSetData instead of mxSetPr, if you are really sure, that the USHORT array is not released from anywhere else. But this is fragile: if the Matlab variable is deleted later, mxFree()'ing the data pointer might cause a crash. I'd prefer memcpy.

Más respuestas (1)

James Tursa
James Tursa el 18 de Feb. de 2022
To attach pointers of type other than double to an mxArray, you can use the mxSetData( ) function as Jan suggests. In your case you would create an mxArray of type mxUINT16_CLASS for this.
However, this will only work if the pointer was allocated with an official API function such as mxMalloc( ) or mxCalloc( ). If the pointer points to memory allocated by standard C/C++ routines such as malloc( ) or calloc( ), or the pointer points to local variables allocated off of the stack, then the use of mxSetData( ) with these pointers will result in an assertion fault and a MATLAB crash. In these cases, you must copy the data with a loop or memcpy( ).
  4 comentarios
Jan
Jan el 18 de Feb. de 2022
Thanks for you explanations.
Wang Yifan
Wang Yifan el 22 de Feb. de 2022
Thanks a lot.

Iniciar sesión para comentar.

Categorías

Más información sobre MATLAB Compiler en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by