Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mat_Open is not working while using matio in C++ #45

Closed
rbnvrw opened this issue Oct 18, 2016 · 6 comments
Closed

Mat_Open is not working while using matio in C++ #45

rbnvrw opened this issue Oct 18, 2016 · 6 comments

Comments

@rbnvrw
Copy link

rbnvrw commented Oct 18, 2016

I am using the matio library to write the contents of a std::vector<std::vector<std::vector<double>>> and some other data to a MatLab file. I used a tutorial here: http://na-wiki.csc.kth.se/mediawiki/index.php/MatIO

However, Mat_Open stays NULL, meaning I can't write anything to the file. There are no error messages, linking/compilation is fine, Saving mat file is never printed and the exit code of the program is 0. Am I missing something obvious?

Contents of the function that should create the MatLab file:

    mat_t *mat;
    matvar_t *matvar;
    unsigned long dims[3] = {100, 100, 3};
    unsigned long single_dim[1] = {1};
    unsigned long size = 100;
    double number = 3.14;

    mat = Mat_Open("/home/ruben/data.mat", MAT_ACC_RDWR);

    if(mat != NULL) {
        std::cout << "Saving mat file" << std::endl;
        matvar = Mat_VarCreate("matrix", MAT_C_DOUBLE, MAT_T_DOUBLE, 3, dims, matrix, 0);
        Mat_VarWrite(mat, matvar, MAT_COMPRESSION_ZLIB);
        Mat_VarFree(matvar);

        matvar = Mat_VarCreate("number", MAT_C_DOUBLE, MAT_T_DOUBLE, 1, single_dim, &number, 0);
        Mat_VarWrite(mat, matvar, MAT_COMPRESSION_ZLIB);
        Mat_VarFree(matvar);

        Mat_Close(mat);
    }

I include the library using #include <matio.h>, and link it with -std=c++11 -L/usr/lib -lmatio -lz. Any help is appreciated.

@tbeu
Copy link
Owner

tbeu commented Oct 18, 2016

Please use Mat_Create or Mat_CreateVer instead of Mat_Open if the file does not exist.

@rbnvrw
Copy link
Author

rbnvrw commented Oct 18, 2016

Thanks for your quick answer. I changed the code like this:

mat = Mat_Create(filename, NULL);

I get the following error message: Process finished with exit code 139 (interrupted by signal 11: SIGSEGV). The data.mat file of 0 bytes is created.

Another question: do you have online documentation available?

@rbnvrw
Copy link
Author

rbnvrw commented Oct 18, 2016

After debugging, I found the answer myself:
matvar = Mat_VarCreate("diffusion_trajectories", MAT_C_DOUBLE, MAT_T_DOUBLE, 3, dims, &trajectories->at(0), 0);

Thanks for your help!

@rbnvrw rbnvrw closed this as completed Oct 18, 2016
@tbeu
Copy link
Owner

tbeu commented Oct 18, 2016

Sure, up-to-date documentation is available from bitbucket.org.

@tbeu
Copy link
Owner

tbeu commented Oct 18, 2016

Data type of dims should be size_t* (not unsigned long*). This likely is the reason for the segmentation violation (as also observed at https://sourceforge.net/p/matio/discussion/609377/thread/e7afc89e).

@tbeu
Copy link
Owner

tbeu commented Oct 19, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants