I am trying to read mat files created by MatLab version 2023a. If I open the files in MatLab I can see all of the variables in the file, which are a mix of structures, arrays numeric and string variables. However, when I attempt to read a variable from the file, the variable name is an empty string. As a result, if I try to read the variable using its name, Mat_VarRead returns NULL.
This is the code I am using:
matvar_t*MatLabUtilities::GetVariableFromMatFileMatio(conststd::string&fileName,conststd::string&variableName){matvar_t*matvar=nullptr;//OpenMAT-filemat_t*matfp=Mat_Open(fileName.c_str(),MAT_ACC_RDONLY);if(matfp==NULL){std::cerr<<"Error opening MAT file"<<std::endl;returnnullptr;}//ReaddatafromMAT-filematvar=Mat_VarRead(matfp,variableName.c_str());if(matvar==NULL){std::cerr<<"Error reading variable from file"<<std::endl;Mat_Close(matfp);returnnullptr;}returnmatvar;}
Any suggestions would be greatly appreciated!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I am trying to read mat files created by MatLab version 2023a. If I open the files in MatLab I can see all of the variables in the file, which are a mix of structures, arrays numeric and string variables. However, when I attempt to read a variable from the file, the variable name is an empty string. As a result, if I try to read the variable using its name, Mat_VarRead returns NULL.
This is the code I am using:
Any suggestions would be greatly appreciated!
Can you attach the MAT file here for reproduction on my side?
Sure, here is the mat file. Really appreciate the help.
Jim
I also added some print statements to the mat.c file to try to get a better understanding of what is happening, and this is what I get:
This is known issue in matio about the opaque data type, see e.g. https://github.com/tbeu/matio/issues/77 .
How did you create the file in MATLAB? Can you use struct as workaround?
Last edit: tbeu 2024-05-02