Debian Codegraph @

matio-1.5.30/documentation/quick.texi

raw
1 @c Copyright (c) 2015-2026, The matio contributors
2 @c Copyright (c) 2011-2014, Christopher C. Hulbert
3 @c All rights reserved.
4 @c
5 @c Redistribution and use in source and binary forms, with or without
6 @c modification, are permitted provided that the following conditions are met:
7 @c
8 @c 1. Redistributions of source code must retain the above copyright notice, this
9 @c list of conditions and the following disclaimer.
10 @c
11 @c 2. Redistributions in binary form must reproduce the above copyright notice,
12 @c this list of conditions and the following disclaimer in the documentation
13 @c and/or other materials provided with the distribution.
14 @c
15 @c THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 @c AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 @c IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 @c DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 @c FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 @c DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 @c SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 @c CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 @c OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 @c OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26 @chapter Quick Start
27 @section Opening and Creating MAT Files
28 This section will show how to create a new MAT file, open an existing MAT file
29 for read and read/write access, and close the MAT file.
30
31 The key functions in working with MAT files include:
32 @itemize
33 @item Mat_Open,
34 @item Mat_CreateVer, and
35 @item Mat_Close.
36 @end itemize
37 The following example program shows how to open a MAT file where the filename
38 is the first argument to the program.
39 @verbatim
40 #include <stdlib.h>
41 #include <stdio.h>
42 #include "matio.h"
43
44 int
45 main(int argc,char **argv)
46 {
47 mat_t *matfp;
48
49 matfp = Mat_Open(argv[1],MAT_ACC_RDONLY);
50 if ( NULL == matfp ) {
51 fprintf(stderr,"Error opening MAT file \"%s\"!\n",argv[1]);
52 return EXIT_FAILURE;
53 }
54
55 Mat_Close(matfp);
56 return EXIT_SUCCESS;
57 }
58 @end verbatim
59
60 The @code{Mat_CreateVer} creates a new MAT file (or overwrites an existing
61 file) with a specific version. The @emph{matio} library can write version 4
62 MAT files, version 5 MAT files, version 5 MAT files with variable
63 compression (if built with zlib), and HDF5 format MAT files introduced in
64 MATLAB version 7.3. The format of the MAT file is specified by the third
65 argument. The short example below creates a version 4 MAT file named
66 @emph{matfile4.mat}, a version 5 MAT file named @emph{matfile5.mat} and an
67 HDF5 format MAT file named @emph{matfile73.mat}.
68
69 @verbatim
70 #include <stdlib.h>
71 #include <stdio.h>
72 #include "matio.h"
73
74 int
75 main(int argc,char **argv)
76 {
77 mat_t *matfp;
78
79 matfp = Mat_CreateVer("matfile4.mat",NULL,MAT_FT_MAT4);
80 if ( NULL == matfp ) {
81 fprintf(stderr,"Error creating MAT file \"matfile4.mat\"!\n");
82 return EXIT_FAILURE;
83 }
84 Mat_Close(matfp);
85
86 matfp = Mat_CreateVer("matfile5.mat",NULL,MAT_FT_MAT5);
87 if ( NULL == matfp ) {
88 fprintf(stderr,"Error creating MAT file \"matfile5.mat\"!\n");
89 return EXIT_FAILURE;
90 }
91 Mat_Close(matfp);
92
93 matfp = Mat_CreateVer("matfile73.mat",NULL,MAT_FT_MAT73);
94 if ( NULL == matfp ) {
95 fprintf(stderr,"Error creating MAT file \"matfile73.mat\"!\n");
96 return EXIT_FAILURE;
97 }
98 Mat_Close(matfp);
99
100 return EXIT_SUCCESS;
101 }
102 @end verbatim
103
104 @section Reading Variables in a MAT File
105 This section introduces the functions used to read variables from a MAT file.
106 The @emph{matio} library has functions for reading variable information only
107 (e.g. name, rank, dimensions, type, etc.), reading information and data, and
108 reading data from previously obtained information. Reading information and data
109 in separate function calls provides several conveniences including:
110 @itemize
111 @item Querying the names of variables in a file without reading data,
112 @item Reading only some fields of a structure or elements of a cell array, and
113 @item other actions where the variable data is not needed.
114 @end itemize
115
116 @subsection Reading a Variable by Name
117 If the name of the variable is known, the @code{Mat_VarRead} and
118 @code{Mat_VarReadInfo} functions can be used. The @code{Mat_VarRead} function
119 reads both the information and data for a variable, and the
120 @code{Mat_VarReadInfo} reads information only. The short example below reads a
121 named variable from a MAT file, and checks that the variable is a complex
122 double-precision vector.
123 @verbatim
124 #include <stdlib.h>
125 #include <stdio.h>
126 #include "matio.h"
127
128 int
129 main(int argc,char **argv)
130 {
131 mat_t *matfp;
132 matvar_t *matvar;
133
134 matfp = Mat_Open(argv[1],MAT_ACC_RDONLY);
135 if ( NULL == matfp ) {
136 fprintf(stderr,"Error opening MAT file \"%s\"!\n",argv[1]);
137 return EXIT_FAILURE;
138 }
139
140 matvar = Mat_VarReadInfo(matfp,"x");
141 if ( NULL == matvar ) {
142 fprintf(stderr,"Variable 'x' not found, or error "
143 "reading MAT file\n");
144 } else {
145 if ( !matvar->isComplex )
146 fprintf(stderr,"Variable 'x' is not complex!\n");
147 if ( matvar->rank != 2 ||
148 (matvar->dims[0] > 1 && matvar->dims[1] > 1) )
149 fprintf(stderr,"Variable 'x' is not a vector!\n");
150 Mat_VarFree(matvar);
151 }
152
153 Mat_Close(matfp);
154 return EXIT_SUCCESS;
155 }
156 @end verbatim
157
158 @subsection Iterating Over Variables in a MAT File
159 For some applications, the name of the variable may not be known ahead of time.
160 For example, if the user needs to select a variable of interest, a list of
161 variables should be obtained. Like reading a variable by name, there are two
162 functions that will read the next variable in the MAT file:
163 @code{Mat_VarReadNext} and @code{Mat_VarReadNextInfo}. The short example shown
164 below opens a MAT file, and iterates over the variables in the file printing
165 the variable name.
166 @verbatim
167 #include <stdlib.h>
168 #include <stdio.h>
169 #include "matio.h"
170
171 int
172 main(int argc,char **argv)
173 {
174 mat_t *matfp;
175 matvar_t *matvar;
176
177 matfp = Mat_Open(argv[1],MAT_ACC_RDONLY);
178 if ( NULL == matfp ) {
179 fprintf(stderr,"Error opening MAT file \"%s\"!\n",argv[1]);
180 return EXIT_FAILURE;
181 }
182
183 while ( (matvar = Mat_VarReadNextInfo(matfp)) != NULL ) {
184 printf("%s\n",matvar->name);
185 Mat_VarFree(matvar);
186 matvar = NULL;
187 }
188
189 Mat_Close(matfp);
190 return EXIT_SUCCESS;
191 }
192 @end verbatim
193
194 @section Writing Variables
195 A variable can be saved in a MAT file using the @code{Mat_VarWrite} function
196 which has three arguments: the MAT file to write the variable to, a MATLAB
197 variable structure, and a third option used to control compression options.
198 The variable structure can be filled in manually, or created from helper
199 routines such as @code{Mat_VarCreate}. Note that MATLAB, and thus @emph{matio},
200 has no concept of a rank 1 array (i.e. vector). The minimum rank of an array is
201 2 (i.e. matrix). A vector is simply a matrix with one dimension length of 1.
202
203 Optionally, a variable can be appended to an existing variable of an HDF5
204 format MAT file by the @code{Mat_VarWriteAppend} function, which takes the
205 same arguments as @code{Mat_VarWrite} as the first three arguments and the
206 dimension as fourth argument. The dimension argument is index 1 based, i.e.,
207 if it is set to @code{d}, the variable is appended along the d-th dimension.
208 If a variable is to be created for later appending, it always must be written
209 by the @code{Mat_VarWriteAppend} function and @code{Mat_VarWrite} must not
210 be called.
211
212 @subsection Writing Numeric Arrays
213 Numeric arrays can be either real or complex. Complex arrays are encapsulated
214 in the @code{struct mat_complex_split_t} data structure that contains a pointer
215 to the real part of the data, and a pointer to the imaginary part of the data.
216 The example program below writes two real variables @emph{x} and @emph{y}, and
217 one complex variable @emph{z} whose real and imaginary parts are the @emph{x}
218 and @emph{y} variables respectively. Note the @code{MAT_F_COMPLEX} argument
219 passed to @code{Mat_VarCreate} for @emph{z} to indicate a complex variable.
220
221 @verbatim
222 #include <stdlib.h>
223 #include <stdio.h>
224 #include "matio.h"
225
226 int
227 main(int argc,char **argv)
228 {
229 mat_t *matfp;
230 matvar_t *matvar;
231 size_t dims[2] = {10,1};
232 double x[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9,10},
233 y[10] = {11,12,13,14,15,16,17,18,19,20};
234 struct mat_complex_split_t z = {x,y};
235
236 matfp = Mat_CreateVer("test.mat",NULL,MAT_FT_DEFAULT);
237 if ( NULL == matfp ) {
238 fprintf(stderr,"Error creating MAT file \"test.mat\"\n");
239 return EXIT_FAILURE;
240 }
241
242 matvar = Mat_VarCreate("x",MAT_C_DOUBLE,MAT_T_DOUBLE,2,dims,x,0);
243 if ( NULL == matvar ) {
244 fprintf(stderr,"Error creating variable for 'x'\n");
245 } else {
246 Mat_VarWrite(matfp,matvar,MAT_COMPRESSION_NONE);
247 Mat_VarFree(matvar);
248 }
249
250 matvar = Mat_VarCreate("y",MAT_C_DOUBLE,MAT_T_DOUBLE,2,dims,y,0);
251 if ( NULL == matvar ) {
252 fprintf(stderr,"Error creating variable for 'y'\n");
253 } else {
254 Mat_VarWrite(matfp,matvar,MAT_COMPRESSION_NONE);
255 Mat_VarFree(matvar);
256 }
257
258 matvar = Mat_VarCreate("z",MAT_C_DOUBLE,MAT_T_DOUBLE,2,dims,&z,
259 MAT_F_COMPLEX);
260 if ( NULL == matvar ) {
261 fprintf(stderr,"Error creating variable for 'z'\n");
262 } else {
263 Mat_VarWrite(matfp,matvar,MAT_COMPRESSION_NONE);
264 Mat_VarFree(matvar);
265 }
266
267 Mat_Close(matfp);
268 return EXIT_SUCCESS;
269 }
270 @end verbatim
271
272 @subsection Writing Cell Arrays
273 Cell arrays are multidimensional arrays whose elements can be any class of
274 variables (e.g. numeric, structure, cell arrays, etc.). To create a cell array,
275 pass an array of @code{matvar_t@tie{}*}. Detailed information on the MATLAB variable
276 structure for cell-arrays is given in @ref{Cell Variables}.
277 The following example shows how to create a 3x1 cell array.
278
279 @verbatim
280 #include <stdlib.h>
281 #include <stdio.h>
282 #include "matio.h"
283
284 int
285 main(int argc,char **argv)
286 {
287 mat_t *matfp;
288 matvar_t *cell_array, *cell_element;
289 size_t dims[2] = {10,1};
290 double x[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9,10},
291 y[10] = {11,12,13,14,15,16,17,18,19,20};
292 struct mat_complex_split_t z = {x,y};
293
294 matfp = Mat_CreateVer("test.mat",NULL,MAT_FT_DEFAULT);
295 if ( NULL == matfp ) {
296 fprintf(stderr,"Error creating MAT file \"test.mat\"\n");
297 return EXIT_FAILURE;
298 }
299
300 dims[0] = 3;
301 dims[1] = 1;
302 cell_array = Mat_VarCreate("a",MAT_C_CELL,MAT_T_CELL,2,dims,NULL,0);
303 if ( NULL == cell_array ) {
304 fprintf(stderr,"Error creating variable for 'a'\n");
305 Mat_Close(matfp);
306 return EXIT_FAILURE;
307 }
308
309 dims[0] = 10;
310 dims[1] = 1;
311 cell_element = Mat_VarCreate(NULL,MAT_C_DOUBLE,MAT_T_DOUBLE,2,dims,x,0);
312 if ( NULL == cell_element ) {
313 fprintf(stderr,"Error creating cell element variable\n");
314 Mat_VarFree(cell_array);
315 Mat_Close(matfp);
316 return EXIT_FAILURE;
317 }
318 Mat_VarSetCell(cell_array,0,cell_element);
319
320 cell_element = Mat_VarCreate(NULL,MAT_C_DOUBLE,MAT_T_DOUBLE,2,dims,y,0);
321 if ( NULL == cell_element ) {
322 fprintf(stderr,"Error creating cell element variable\n");
323 Mat_VarFree(cell_array);
324 Mat_Close(matfp);
325 return EXIT_FAILURE;
326 }
327 Mat_VarSetCell(cell_array,1,cell_element);
328
329 cell_element = Mat_VarCreate(NULL,MAT_C_DOUBLE,MAT_T_DOUBLE,2,dims,&z,
330 MAT_F_COMPLEX);
331 if ( NULL == cell_element ) {
332 fprintf(stderr,"Error creating cell element variable\n");
333 Mat_VarFree(cell_array);
334 Mat_Close(matfp);
335 return EXIT_FAILURE;
336 }
337 Mat_VarSetCell(cell_array,2,cell_element);
338
339 Mat_VarWrite(matfp,cell_array,MAT_COMPRESSION_NONE);
340 Mat_VarFree(cell_array);
341
342 Mat_Close(matfp);
343
344 return EXIT_SUCCESS;
345 }
346 @end verbatim
347
348 @subsection Writing Structure Arrays
349 Structure arrays are multidimensional arrays where each element of the array
350 contains multiple data items as named fields. The fields of a structure can
351 be accessed by name or index. A field can be a variable of any type (e.g.
352 numeric, structure, cell arrays, etc.). The preferred method to create a
353 structure array is using the @code{Mat_VarCreateStruct2} function. After creating
354 the structure array, the @code{Mat_VarSetStructFieldByName} and
355 @code{Mat_VarSetStructFieldByIndex} functions can be used to set the fields of
356 the structure array to a variable. The example below shows how to create a
357 2 x 1 structure array with the fields @emph{x}, @emph{y}, and @emph{z}.
358
359 @verbatim
360 #include <stdlib.h>
361 #include <stdio.h>
362 #include "matio.h"
363
364 int
365 main(int argc,char **argv)
366 {
367 mat_t *matfp;
368 matvar_t *matvar, *field;
369 size_t dims[2] = {10,1}, struct_dims[2] = {2,1};
370 double x1[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9,10},
371 x2[10] = {11,12,13,14,15,16,17,18,19,20},
372 y1[10] = {21,22,23,24,25,26,27,28,29,30},
373 y2[10] = {31,32,33,34,35,36,37,38,39,40};
374 struct mat_complex_split_t z1 = {x1,y1}, z2 = {x2,y2};
375 const char *fieldnames[4] = {"x","y","z",NULL};
376
377 matfp = Mat_CreateVer("test.mat",NULL,MAT_FT_DEFAULT);
378 if ( NULL == matfp ) {
379 fprintf(stderr,"Error creating MAT file \"test.mat\"\n");
380 return EXIT_FAILURE;
381 }
382
383 matvar = Mat_VarCreateStruct2("a", 2,struct_dims,fieldnames);
384 if ( NULL == matvar ) {
385 fprintf(stderr,"Error creating variable for 'a'\n");
386 Mat_Close(matfp);
387 return EXIT_FAILURE;
388 }
389
390 /* structure index 0 */
391 field = Mat_VarCreate(NULL,MAT_C_DOUBLE,MAT_T_DOUBLE,2,dims,x1,0);
392 Mat_VarSetStructFieldByName(matvar,"x",0,field);
393 field = Mat_VarCreate(NULL,MAT_C_DOUBLE,MAT_T_DOUBLE,2,dims,y1,0);
394 Mat_VarSetStructFieldByName(matvar,"y",0,field);
395 field = Mat_VarCreate(NULL,MAT_C_DOUBLE,MAT_T_DOUBLE,2,dims,&z1,
396 MAT_F_COMPLEX);
397 Mat_VarSetStructFieldByName(matvar,"z",0,field);
398
399 /* structure index 1 */
400 field = Mat_VarCreate(NULL,MAT_C_DOUBLE,MAT_T_DOUBLE,2,dims,x2,0);
401 Mat_VarSetStructFieldByName(matvar,"x",1,field);
402 field = Mat_VarCreate(NULL,MAT_C_DOUBLE,MAT_T_DOUBLE,2,dims,y2,0);
403 Mat_VarSetStructFieldByName(matvar,"y",1,field);
404 field = Mat_VarCreate(NULL,MAT_C_DOUBLE,MAT_T_DOUBLE,2,dims,&z2,
405 MAT_F_COMPLEX);
406 Mat_VarSetStructFieldByName(matvar,"z",1,field);
407
408 Mat_VarWrite(matfp,matvar,MAT_COMPRESSION_NONE);
409 Mat_VarFree(matvar);
410
411 Mat_Close(matfp);
412 return EXIT_SUCCESS;
413 }
414 @end verbatim