tbeu / matio

MATLAB MAT File I/O Library
https://matio.sourceforge.io
BSD 2-Clause "Simplified" License
330 stars 97 forks source link

Not really an issue: Add a function Mat_GetFileMode to the library. #194

Closed mkostrun closed 8 months ago

mkostrun commented 1 year ago

Please add this simple function, because struct's elements are private to the library.

-> to mat.c; and its header to mat.h int Mat_GetFileMode(mat_t *mat) { int rval=-1; if ( NULL != mat ) rval = mat->mode;

return rval; }

mkostrun commented 1 year ago

Another comment: could you modify mat.c/L.481 to: mat = Mat_CreateVer(matname, NULL, (enum mat_ft)((MAT_FT_DEFAULT|mode) & 0xfffffffe)); so that Mat_Open(filename,mode) does not fail if the file does not exist, and user requested RDWR access to it.

Along the same lines, can the file mode flags be expanded to:

  1. append mode (if file does not exist, create it; start writing at the end of it)
  2. write mode (if file exist, delete its previous content and start new one). see https://stackoverflow.com/questions/21113919/difference-between-r-and-w-in-fopen: flag "r+" will not create a file for RDWR if it does not exist.
  3. read mode (if file does not exist, report an error - already done with MAT_ACC_RDONLY)

write mode is needed for repeated execution of a script to create the same mat file with certain data in it.

tbeu commented 1 year ago

Sure, can do. Feel free to send in a PR if you like.

mkostrun commented 1 year ago

Pardon my ignorance, but what is PR?

I have a question for my rlab interface to matio: I crate mat file and put some variables in it, then close it. Can I append to that file and add more content, or not?

Consider rlab example: matopen(fn=“myfile.mat”,”rw”); matwritem(fn,x=rand(3,8)); matclose(fn); If I repeat code above with another variable, say y, then only the last data will be in the file. —————————

On Oct 13, 2022, at 06:42, tbeu @.***> wrote:

 Sure, can do. Feel free to send in a PR if you like.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.

tbeu commented 1 year ago

Pardon my ignorance, but what is PR?

Never mind, PR is for pull request to propose changes.

I have a question for my rlab interface to matio: I crate mat file and put some variables in it, then close it. Can I append to that file and add more content, or not? Consider rlab example: matopen(fn=“myfile.mat”,”rw”); matwritem(fn,x=rand(3,8)); matclose(fn); If I repeat code above with another variable, say y, then only the last data will be in the file.

See #60.

tbeu commented 10 months ago

Please add this simple function, because struct's elements are private to the library.

-> to mat.c; and its header to mat.h int Mat_GetFileMode(mat_t *mat) { int rval=-1; if ( NULL != mat ) rval = mat->mode;

return rval; }

Thinking once again I'd rather not expose the file open mode and keep it an internal flag. Even worse, it is used inconsistently within the library and seems to be broken:

  1. Mat_Create4 sets mode to MAT_ACC_RDONLY, whereas Mat_Create5 and Mat_Create73 set it to MAT_ACC_RDWR.
  2. Mat_Close does not reset the mode. Having MAT_ACC_RDONLY set to zero as default seems unfortunate.
  3. Mat_VarDelete does not respect the mode, i.e. it is possible to delete a variable from a file that is opened as MAT_ACC_RDONLY. Mat_VarDelete seems to be the only location where mode is utilized at all, but only when copying the mode flag from the existing file to the new file.

Another comment: could you modify mat.c/L.481 to: mat = Mat_CreateVer(matname, NULL, (enum mat_ft)((MAT_FT_DEFAULT|mode) & 0xfffffffe)); so that Mat_Open(filename,mode) does not fail if the file does not exist, and user requested RDWR access to it.

Mat_Open is documented to only work on existing files. I'd rather keep it as is.

tbeu commented 8 months ago

I added Mat_GetFileAccessMode to the public API and fixed the above mentioned issued by ad9cc50.