File: Mat_VarCreateStruct2.3

package info (click to toggle)
libmatio 1.5.30-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 34,656 kB
  • sloc: sh: 126,066; ansic: 22,630; makefile: 647; python: 215
file content (104 lines) | stat: -rw-r--r-- 3,610 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
.\" Copyright (c) 2015-2026, The matio contributors
.\" Copyright (c) 2012-2014, Christopher C. Hulbert
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions are met:
.\"
.\" 1. Redistributions of source code must retain the above copyright notice, this
.\"    list of conditions and the following disclaimer.
.\"
.\" 2. Redistributions in binary form must reproduce the above copyright notice,
.\"    this list of conditions and the following disclaimer in the documentation
.\"    and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
.\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
.\" DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
.\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
.\" CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
.\" OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd January 5, 2026
.Dt MAT_VARCREATESTRUCT2 3
.Os
.Sh NAME
.Nm Mat_VarCreateStruct2
.Nd Creates a structure variable.
.Sh SYNOPSIS
.Fd #include <matio.h>
.Ft matvar_t *
.Fo Mat_VarCreateStruct
.Fa "const char *name"
.Fa "int rank"
.Fa "const size_t *dims"
.Fa "const char *const *fields"
.Fc
.Sh DESCRIPTION
The
.Fn Mat_VarCreateStruct2
function creates a structure variable named
.Fa name
that can be written to a MAT file.
.Sh RETURN VALUES
If the structure variable was successfully created, a pointer to the variable
is returned.
Otherwise NULL is returned.
The structure variable pointer should be free'd when no longer needed using
.Fn Mat_VarFree .
The names of the fields are copied in the function, and thus should be released
after calling the function if necessary.
The names of the fields has to be a NULL-terminated array.
.Sh EXAMPLES
This example program opens a MAT file named by the first argument to the
program, and writes a structure named
.Em a
to the file.
.Bd -literal
#include "matio.h"

int
main(int argc, char **argv)
{
    mat_t    *matfp;
    matvar_t *matvar;
    matvar_t *field;
    const char *fields[3] = {"field1", "field2", NULL};
    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 %s\n", argv[1]);
        return EXIT_FAILURE;
    }

    dims[0] = 1; dims[1] = 1;
    matvar = Mat_VarCreateStruct2("a", 2, dims, fields);
    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;
}
.Ed
.Sh SEE ALSO
.Xr Mat_VarCreate 3 ,
.Xr Mat_VarSetStructFieldByName 3