Add support for reading and writing variables in row major order
Brought to you by:
chulbe2lsu,
t-beu
If I have not missed something, matio adheres to Matlab column-major convention for storing variables data. Row-major order could be supported when reading, as a a sticky flag in mat_t structure (so all read variables are consistent) and as a option in all variable creation methods, as for example in Mat_VarCreate.
I'd rather see it as flag for Mat_VarRead or Mat_VarWrite.
What is the problem of the manual workaround with array transposition? There even are algorithms for in-place transposition to avoid temporary double storage.
I tried to propose an approach that is not API breaking. There's no problem with array transposition but:
1. The library is currently a bit opaque about the storage ordering convention. Of course, seeing no mention about it, one has to think that it will be same as in Matlab and it's actually the case. Seeing both methods supported would help in API clarity;
2. in place transposition is not trivial algorithm (requires O(log MN) auxiliary storage and computation cost is O(MN log M*N) in the worst acase) and I attempted to code it myself before realizing it was not. Instead supporting both ordering methods it in the library would be completely trivial. I'm not concerned about performance here: I'm just saying that the API would be nicer if it had this feature built-in.
Actually the array is written row-major, same way as C arrays are stored. For this reason, to have it compatible with MATLAB, you need to transpose the array yourself to force col-major storage when writing to file. See e.g., https://github.com/tbeu/matio/blob/master/src/mat4.c#L195
I'd rather add new functions Mat_VarRead2and Mat_VarWrite2 to not break the API.
What you write here is confusting to me but maybe we are saying the same thing. This is my observations:
- If I read a matrix from a .mat and I save it again as a .mat without any modification (with matio ofc), matlab will read it correctly;
- If I read a matrix from a .mat and I want to use it in math library that uses row-major convention (e.g. OpenCV) I have to transpose it. If I want to save it as .mat again I have to transpose it again before writing it with matio.
As to per your suggestion of introducing Mat_VarRead2 and Mat_VarWrite2: Mat_VarRead2 seems fine and matvar_t could have a field stating if the the order is column or row major to hint in case of inconsistent reads. Maybe Mat_VarWrite2 could be not enough fine grained? For example one may have a struct with many fields and Mat_VarWrite2 would not be enough to specify different conventions on different fields. That's why I suggested to put the option in Mat_VarCreate.
Last edit: Francesco Pretto 2017-08-05
Yes, your observations are right:
The fields of a struct are of matvar_t, too, and can therefore have different flags for col-major or row-major storrage. Thus, no problem to deal with it.
Last edit: tbeu 2017-08-08