matio icon indicating copy to clipboard operation
matio copied to clipboard

Mat_VarCreate states minimum rank is 2, but 1 seems to work.

Open kargl opened this issue 1 year ago • 2 comments

The manual page contains

 The rank argument specifies how many dimensions the data has.  The minimum rank is 2.

The below program sets rank=1 and dims[1] = {1}. When compiled and run with

  % cc -o z b.c -I/usr/local/include -L/usr/local/lib -lmatio -lz
  % ./z z.mat

the file z.mat loaded into octave contains the right variable and value. Is the documentation wrong?

#include <err.h>
#include <stdlib.h>
#include <stdio.h>

#include "matio.h"

#define COMP MAT_COMPRESSION_NONE

void
write_float_scalar(mat_t *mfp, char *xnam, float *xval)
{
   matvar_t *mvar;
   int rank = 1;       // Should a run-time error occur because rank = 1?
   size_t dims[1] = {1};
   mvar = Mat_VarCreate(xnam, MAT_C_SINGLE, MAT_T_SINGLE, rank, dims, xval, 0);
   if (!mvar)
     errx(1, "creating variable: %s", xnam);
   Mat_VarWrite(mfp, mvar, COMP);
   Mat_VarFree(mvar);
}

int
main(int argc, char *argv[])
{
   mat_t *mfp;
   matvar_t *mvar;
   int i,j;
   float  a;
   size_t dims[2];

   if (argc != 2)
      errx(1,"Usage: a filename");

   mfp = Mat_CreateVer(*++argv, NULL, MAT_FT_MAT5);
   if (!mfp)
      errx(1, "cannot create %s", *argv);

   a = 42;
   write_float_scalar(mfp, "a", &a);

   Mat_Close(mfp);

   return 0;
}

kargl avatar Oct 31 '24 00:10 kargl

No feedback.

kargl avatar Apr 15 '25 19:04 kargl

Will be handled by next release. Thanks for reporting.

tbeu avatar Apr 15 '25 19:04 tbeu

Should be fixed now:

  • I removed the minimum rank limitation from the docs. For MAT file v5 and v7,3 creating variables of rank = 1 works as you already mentioned. v4 always requires rank = 2.
  • I added many tests.
  • I fixed Mat_VarPrint for rank = 1 variables.

tbeu avatar Oct 26 '25 16:10 tbeu

Thank you for the fix. While my use case for matio is restricted to a rather simple IO, I have found matio a big help in providing portable output to my colleagues.

kargl avatar Oct 26 '25 17:10 kargl