MAT_REWIND(3)

πŸ’– MAT_REWIND(3) πŸ’–

okay bestie, welcome to the Library Functions Manual β€” this is the MAT_REWIND page and it's honestly such a tiny little icon, one job, does it perfectly. ✨


🌸 NAME 🌸

Mat_Rewind β€” rewinds an open MAT file all the way back to the beginning. that's it. that's the whole gig. πŸ’…


πŸŽ€ SYNOPSIS πŸŽ€

#include <matio.h>

int
Mat_Rewind(mat_t *matfp);

πŸ¦‹ DESCRIPTION πŸ¦‹

so like. you've got an open MAT file, right? and you've been reading through it, vibing, consuming variables one by one. and now you wanna go back to the start. not me rewinding the whole thing like a cassette tape 🎧

Mat_Rewind rewinds an open MAT file so that Mat_VarReadNext() or Mat_VarReadNextInfo() reads the first variable in the file. that's the entire plot. main character energy, minimal runtime, zero filler. πŸ‘‘


✨ RETURN VALUES ✨

  • 0 β€” success, we ate, the file is rewound, slay πŸ’–
  • -1 β€” failure 😭 something went sideways, no rewind for you

πŸ“ EXAMPLES πŸ“

ok so here's the cutest little program. it lists out all the variables in the MAT file, asks you which one you want, and if your index is actually valid it resets the file to the beginning and reads forward until it hits your chosen index. lowkey elegant ngl 🫢

#include <stdlib.h>
#include <stdio.h>
#include "matio.h"

int
main(int argc, char **argv)
{
    mat_t    *matfp;
    matvar_t *matvar;
    int       idx, num_variables;

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

    idx = 0;
    while ( NULL != (matvar = Mat_VarReadNextInfo(matfp)) ) {
        idx++;
        printf("%3d. %s0, idx, matvar->name);
        Mat_VarFree(matvar);
    }
    num_variables = idx;
    if ( num_variables > 0 ) {
        printf("Which variable would you like to read? ");
        if ( 0 == fscanf(stdin,"%d",&idx) ) {
            printf("Invalid variable selection!0);
        } else if ( idx < 1 || idx > num_variables ) {
            fprintf(stderr, "That is an invalid variable index!0);
        } else {
            int k;
            Mat_Rewind(matfp);
            for ( k = 1; k < idx; k++ ) {
                matvar = Mat_VarReadNextInfo(matfp);
                Mat_VarFree(matvar);
            }
            matvar = Mat_VarReadNext(matfp);
            Mat_VarPrint(matvar, 1);
            Mat_VarFree(matvar);
        }
    }

    Mat_Close(matfp);
    return EXIT_SUCCESS;
}

see the moment? right there in the middle β€” Mat_Rewind(matfp); β€” file goes whoosh back to the top and then we count forward to the one you asked for. iconic behaviour, truly. 🌈


πŸ”₯ SEE ALSO πŸ”₯


and that's the whole page bestie!! this little drop of sunshine came from Debian, dated September 12, 2019. go forth and rewind responsibly πŸ’œπŸ§šβœ¨

πŸ“¦ where this page comes from