Accessing struct using C library always NULL

5 visualizaciones (últimos 30 días)
Beig Serate
Beig Serate el 12 de En. de 2022
Comentada: Beig Serate el 13 de En. de 2022
Hi,
I am using the Matlab C interface to extract data out of a .mat file containing a 1x50 struct. I am able to do the following successfully:
  • Pull structure using matGetNextVariableInfo. Also fills the char* with the name, which matches my struct variable
  • Get the correct size of the struct by using mxGetNumberOfElements and mxGetNumberOfFields
  • Get the correct first column name by using mxGetFieldNameByNumber
  • Get the first field using mxGetField
  • Retrieve the correct class name (in my case, a double) from mxGetClassName
After calling mxGetField, I retrieve a non-null pointer to a mxArray, I then try to call mxGetPr on this - but it always ends up with a NULL result. I also tried mxGetData to retrieve a void*, also NULL. I am able to verify that it is not empty by using mxIsEmpty. If I change the row index to one with an empty field, mxIsEmpty returns as expected. It seems like I have everything set up correctly, but nothing I do works for retrieving the actual field data.
Any help is appreciated. Thanks!

Respuesta aceptada

James Tursa
James Tursa el 13 de En. de 2022
Editada: James Tursa el 13 de En. de 2022
Typically, it is best just to post your code so we can see exactly what you are doing, instead of posting a description of your code for what you think it is doing.
That being said, the matGetNextVariableInfo( ) function just gets header information for the variable without getting the actual data of the variable. It is intended for you to quickly see what is there without loading potentially a very large variable. So seeing NULL behind a subsequent mxGetPr( ) or mxGetData( ) call would be expected. If you want to get the variable along with its data, you need to use matGetNextVariable( ) instead.
  1 comentario
Beig Serate
Beig Serate el 13 de En. de 2022
Understood about the code - will definitely do that in the future. But like you said, using matGetNextVariableInfo() was my issue. Thank you for the quick reply and help!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by