---
name: "Mat_VarCreate"
section: "3"
platform: "linux"
package: "libmatio"
version: "1.5.30-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/Mat_VarCreate"
---

# Mat\_VarCreate(3)

Creates a MAT variable structure.

## NAME

**Mat\_VarCreate** – Creates a MAT variable structure.

## SYNOPSIS

**#include \<matio.h\>**

*matvar\_t* *\**\

**Mat\_VarCreate**(*const char \*name*, *enum matio\_classes class\_type*, *enum matio\_types data\_type*, *int rank*, *const size\_t \*dims*, *const void \*data*, *int opt*);

## DESCRIPTION

The **Mat\_VarCreate**() function creates a MAT structure variable named *name* that can be written to a MAT file. The *class\_type* argument specifies the class of the variable, and the *data\_type* argument specifies the type of the data. For example, a double-precision class would use MAT\_C\_DOUBLE for the class type and MAT\_T\_DOUBLE for the data type. In some instances, the data type may not match the class type. For example, an array of integers can be written in the double-precision class by using MAT\_T\_INT32 for *data\_type.*

The *rank* argument specifies how many dimensions the data has. The number of elements in each dimension is specified in the array *dims.*

The *data* argument is a pointer to the variable data. The pointer is typically a pointer to a numeric array (e.g. double, float, int, etc.) for real variables. For complex variables, the pointer is a pointer to a *mat\_complex\_split\_t* which contains pointers to the real and imaginary data as fields of the structure. For sparse variables, the pointer should be a *mat\_sparse\_t* *\*.*

## RETURN VALUES

If the variable was successfully created, a pointer to the variable is returned. Otherwise NULL is returned. The variable should be free'd when no longer needed using **Mat\_VarFree**().

## EXAMPLES

The example program below creates a MAT file named *test.mat,* and writes two real numeric variables *x* and *y* and a complex variable *z* to the file.

```
#include <stdlib.h>
#include <stdio.h>
#include "matio.h"
int
main(int argc,char **argv)
{
    mat_t    *matfp;
    matvar_t *matvar;
    size_t    dims[2] = {10,1};
    double    x[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9,10},
              y[10] = {11,12,13,14,15,16,17,18,19,20};
    struct mat_complex_split_t z = {x, y};
    matfp = Mat_CreateVer("test.mat", NULL, MAT_FT_DEFAULT);
    if ( NULL == matfp ) {
        fprintf(stderr, "Error creating MAT file
        return EXIT_FAILURE;
    }
    matvar = Mat_VarCreate("x", MAT_C_DOUBLE, MAT_T_DOUBLE, 2, dims, x, 0);
    if ( NULL == matvar ) {
        fprintf(stderr, "Error creating variable for 'x'0);
    } else {
        Mat_VarWrite(matfp, matvar, MAT_COMPRESSION_NONE);
        Mat_VarFree(matvar);
    }
    matvar = Mat_VarCreate("y", MAT_C_DOUBLE, MAT_T_DOUBLE, 2, dims, y, 0);
    if ( NULL == matvar ) {
        fprintf(stderr, "Error creating variable for 'y'0);
    } else {
        Mat_VarWrite(matfp, matvar, MAT_COMPRESSION_NONE);
        Mat_VarFree(matvar);
    }
    matvar = Mat_VarCreate("z", MAT_C_DOUBLE, MAT_T_DOUBLE, 2, dims, &z,
                 MAT_F_COMPLEX);
    if ( NULL == matvar ) {
        fprintf(stderr, "Error creating variable for 'z'0);
    } else {
        Mat_VarWrite(matfp, matvar, MAT_COMPRESSION_NONE);
        Mat_VarFree(matvar);
    }
    Mat_Close(matfp);
    return EXIT_SUCCESS;
}
```

## SEE ALSO

[Mat\_VarCreateStruct(3)](/Mat_VarCreateStruct/3.md), [Mat\_VarFree(3)](/Mat_VarFree/3.md), [Mat\_VarWrite(3)](/Mat_VarWrite/3.md)
