MAT_VARCREATESTRUCT(3)

πŸ’– MAT_VARCREATESTRUCT(3) πŸ’–

okay bestie, welcome to the Library Functions Manual page for Mat_VarCreateStruct 🫢 this is a matio situation and we're gonna get through it together, no cap.


✨ NAME ✨

Mat_VarCreateStruct β€” literally creates a structure variable. that's the whole job. queen behavior.


🌸 SYNOPSIS 🌸

#include <matio.h>

matvar_t *
Mat_VarCreateStruct(const char *name, int rank, const size_t *dims,
    const char *const *fields, unsigned nfields);

πŸ¦‹ DESCRIPTION πŸ¦‹

so the Mat_VarCreateStruct() function creates a structure variable named name that can be written to a MAT file. that's it, that's the vibe. you hand it a name, a rank, some dims, a list of fields and how many of them there are (nfields), and it gives you back a whole structure variable. iconic.


πŸ’… RETURN VALUES πŸ’…

if the structure variable was successfully created, you get a pointer to the variable back. slay. πŸŽ€

otherwise you get NULL. no variable, no vibes, nothing. πŸ’€

the structure variable pointer should be free'd when no longer needed, using Mat_VarFree(). don't forget, bestie, that's a memory leak otherwise and we do not do that here.

also important: the names of the fields are copied in the function, and thus should be released after calling the function if necessary. so your originals are yours to deal with.

🚨 okay serious moment: this function is deprecated since version 1.5.30. use Mat_VarCreateStruct2 instead. i'm not saying it to be mean, i'm saying it because i love you. πŸ’œ

🌈 EXAMPLES 🌈

this example program opens a MAT file named by the first argument to the program, and writes a structure named a to the file. let's gooo.

#include "matio.h"

int
main(int argc, char **argv)
{
    mat_t    *matfp;
    matvar_t *matvar;
    matvar_t *field;
    const char *fields[2] = {"field1", "field2"};
    double       data1 = 1, data2 = 2;
    size_t       dims[2] = {1, 1};

    matfp = Mat_Open(argv[1], MAT_ACC_RDWR);
    if ( NULL == matfp ) {
        fprintf(stderr, "Error opening MAT file %s0, argv[1]);
        return EXIT_FAILURE;
    }

    dims[0] = 1; dims[1] = 1;
    matvar = Mat_VarCreateStruct("a", 2, dims, fields, 2);
    if ( NULL == matvar ) {
        Mat_Close(matfp);
        return EXIT_FAILURE;
    }

    field = Mat_VarCreate(NULL, MAT_C_DOUBLE, MAT_T_DOUBLE, 2, dims, &data1,
                MAT_F_DONT_COPY_DATA);
    Mat_VarSetStructFieldByName(matvar, "field1", 0, field);

    field = Mat_VarCreate(NULL, MAT_C_DOUBLE, MAT_T_DOUBLE, 2, dims, &data2,
                MAT_F_DONT_COPY_DATA);
    Mat_VarSetStructFieldByName(matvar, "field2", 0, field);

    Mat_VarWrite(matfp, matvar, MAT_COMPRESSION_NONE);
    Mat_VarFree(matvar);

    Mat_Close(matfp);
    return EXIT_SUCCESS;
}

see how it all comes together? open, create, set the fields, write, free, close. a whole little routine and it ate. πŸ”₯


πŸ‘‘ SEE ALSO πŸ‘‘


and that's the page, bestie πŸ’– this one came to you from Debian, dated January 5, 2026, and it lives under the name MAT_VARCREATESTRUCT(3). go forth and make structures. mwah. πŸŽ€βœ¨

πŸ“¦ where this page comes from