1MAT_REWIND(3) BSD Library Functions Manual MAT_REWIND(3)
2
4 Mat_Rewind — Rewinds an open MAT file to the beginning.
5
7 #include <matio.h>
8
9 int
10 Mat_Rewind(mat_t *matfp);
11
13 Rewinds an open MAT file so that Mat_VarReadNext() or
14 Mat_VarReadNextInfo() reads the first variable in the file.
15
17 The function returns 0 on success, or -1 on failure.
18
20 The following example shows a list of variables in the MAT file, and
21 prompts the user for the index of the variable to read. If the index is
22 valid, the file is reset to the beginning, and variables read until the
23 selected index is reached.
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include "matio.h"
28
29 int
30 main(int argc, char **argv)
31 {
32 mat_t *matfp;
33 matvar_t *matvar;
34 int idx, num_variables;
35
36 matfp = Mat_Open(argv[1], MAT_ACC_RDONLY);
37 if ( NULL == matfp ) {
38 fprintf(stderr, "Error opening MAT file %s0, argv[1]);
39 return EXIT_FAILURE;
40 }
41
42 idx = 0;
43 while ( NULL != (matvar = Mat_VarReadNextInfo(matfp)) ) {
44 idx++;
45 printf("%3d. %s0, idx, matvar->name);
46 Mat_VarFree(matvar);
47 }
48 num_variables = idx;
49 if ( num_variables > 0 ) {
50 printf("Which variable would you like to read? ");
51 if ( 0 == fscanf(stdin,"%d",&idx) ) {
52 printf("Invalid variable selection!0);
53 } else if ( idx < 1 || idx > num_variables ) {
54 fprintf(stderr, "That is an invalid variable index!0);
55 } else {
56 int k;
57 Mat_Rewind(matfp);
58 for ( k = 1; k < idx; k++ ) {
59 matvar = Mat_VarReadNextInfo(matfp);
60 Mat_VarFree(matvar);
61 }
62 matvar = Mat_VarReadNext(matfp);
63 Mat_VarPrint(matvar, 1);
64 Mat_VarFree(matvar);
65 }
66 }
67
68 Mat_Close(matfp);
69 return EXIT_SUCCESS;
70 }
71
73 Mat_Open(3), Mat_Close(3), Mat_VarReadNext(3), Mat_VarReadNextInfo(3),
74 Mat_VarPrint(3)
75
76BSD September 12, 2019 BSD