Menu

Can Matio read mat files created by version R2023a?

Help
2024-05-02
2024-05-02
  • Jim Houskeeper

    Jim Houskeeper - 2024-05-02

    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(const std::string& fileName, const std::string& variableName)
    {
        matvar_t *matvar = nullptr;
    
        // Open MAT-file
        mat_t *matfp = Mat_Open(fileName.c_str(), MAT_ACC_RDONLY);
        if (matfp == NULL) {
            std::cerr << "Error opening MAT file" << std::endl;
            return nullptr;
        }
    
        // Read data from MAT-file
        matvar = Mat_VarRead(matfp, variableName.c_str());
        if (matvar == NULL) {
            std::cerr << "Error reading variable from file" << std::endl;
            Mat_Close(matfp);
            return nullptr;
        }
        return matvar;
    }
    

    Any suggestions would be greatly appreciated!

     
  • tbeu

    tbeu - 2024-05-02
    • Yes, MATLAB R2023a is supported.
    • Code snippet LGTM.

    Can you attach the MAT file here for reproduction on my side?

     
  • Jim Houskeeper

    Jim Houskeeper - 2024-05-02

    Sure, here is the mat file. Really appreciate the help.

    Jim

     
  • Jim Houskeeper

    Jim Houskeeper - 2024-05-02

    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:

    File = mat.c, Line = 2759, mat->version = 0x100
    File = mat.c, Line = 2718, class type = 17, matvar->name = (null), name = stereoParams
    File = mat.c, Line = 2718, class type = 17, matvar->name = (null), name = stereoParams
    File = mat.c, Line = 2718, class type = 9, matvar->name = , name = stereoParams
    File = mat.c, Line = 2777, Mat_VarReadInfo failed, returning NULL
    
     
  • tbeu

    tbeu - 2024-05-02

    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

Log in to post a comment.

MongoDB Logo MongoDB