Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for reading Matlab Objects from matfile V5 #64

Closed
wants to merge 6 commits into from

Conversation

hernot
Copy link

@hernot hernot commented Jun 14, 2017

The official version is not able to read matlab files using matfile V5
format, which contain matlab varaibles eposing a variable type of
MAT_C_OBJECT. Fields, arrays and cells of this type are reported to be
emtpy.

From documentation of matfile format V5 variables,fields, arrays and
cells of type MAT_C_OBJECT only fiffer from their MAT_C_STRUCT silblings
only by an additional field in the variable entry header stating the
name of the corresponding MATLAB class. This allows to implement the
support for reading MAT_C_OBJECT type variables by using following
strategy:

1) Wherever possible extend type checks such that operation to be

exectued on MAT_C_OBJECT variables is handled equal to MAT_C_STRUCT type
2) Keep MAT_C_OBJECT specific code as small as possible and resort
to MAT_C_STRUCT path as soon as there is no difference between handling
either.

The changes were originally implemented by this author for an initial
release of matio 1.5.3, used by data conversion tool of
g.tec (www.gtec.at) to allow reading data generated by the commercial
MATLAB and Simulink based tools.

Implementation is based upon pull from git://git.code.sf.net/p/matio/matio on
Sat May 27 13:21:32 2017

hernot and others added 2 commits June 14, 2017 14:24
The official version is not able to read matlab files using matfile V5
format, which contain matlab varaibles eposing a variable type of
MAT_C_OBJECT. Fields, arrays and cells of this type are reported to be
emtpy.

From documentation of matfile format V5 variables,fields, arrays and
cells of type MAT_C_OBJECT only fiffer from their MAT_C_STRUCT silblings
only by an additional field in the variable entry header stating the
name of the corresponding MATLAB class. This allows to implement the
support for reading MAT_C_OBJECT type variables by using following
strategy:

    1) Wherever possible extend type checks such that operation to be
exectued on MAT_C_OBJECT variables is handled equal to MAT_C_STRUCT type
    2) Keep MAT_C_OBJECT specific code as small as possible and resort
to MAT_C_STRUCT path as soon as there is no difference between handling
either.

The changes were originally implemented by this author for an initial
release of matio 1.5.3, used by data conversion tool of
g.tec (www.gtec.at) to allow reading data generated by the commercial
MATLAB and Simulink based tools.
…into hernot-read-matlab5-object

# Conflicts:
#	src/mat5.c
@tbeu
Copy link
Owner

tbeu commented Jun 15, 2017

Thanks for this PR. I merged your branch on top of recent master (and hopefully got the merge conflicts right).

  • There is classname added to matvar_t, which changes the public API. Instead I prefer to have it in struct matvar_internal, where we are free of backward compatibility breaking changes.
  • There are some TODOs added. Are you going to do them?
  • Why did you add Mat_GetHeader to the public API? It is of course nice to have it, but I wonder, since it is not needed as part of your PR.

@hernot
Copy link
Author

hernot commented Jun 15, 2017

Regarding class_name field of matvar_t moving to matvar_internal. Is ok for me as long as you advise me how an appropriate function should look like to access and print the name of the matlab class definition should look like when it is stored on matvar_internal.

Regarding the TODO's I have put them as i would need your review whether the if conditions would be sufficient or whether in the following code there would be any extra code necessary. For now it seems that the code as is would be ok. But i currentl do not have access to the data and facility to throughly test them, and verify that that these two reminder todos can be solved by deleting them, which would be the best case.

@tbeu
Copy link
Owner

tbeu commented Jun 15, 2017

OK, I will move class_name member and check the TODOs in more detail.

Repository owner deleted a comment from coveralls Jun 15, 2017
Repository owner deleted a comment from coveralls Jun 15, 2017
Repository owner deleted a comment from coveralls Jun 15, 2017
Repository owner deleted a comment from coveralls Jun 15, 2017
Repository owner deleted a comment from coveralls Jun 15, 2017
Repository owner deleted a comment from coveralls Jun 15, 2017
Repository owner deleted a comment from coveralls Jun 15, 2017
Repository owner deleted a comment from coveralls Jun 15, 2017
@tbeu
Copy link
Owner

tbeu commented Jun 16, 2017

This is what I tried:

  1. MATLAB commands
inline_object = inline('t^2');
memmap_object = memmapfile('c:\temp\mem.map');
save object_uncompressed -v6 inline_object memmap_object
  1. MAT-File
    mat.zip

  2. Matdump command

matdump -d -v object_uncompressed.mat
  1. matdump output
    https://gist.github.com/tbeu/f0b077df1c278a4687b8aa4b9640d967

Thus, it seems to work on the inline object, but not on the memmapfile object. What kind of objects did you build?

Repository owner deleted a comment from coveralls Jun 16, 2017
Repository owner deleted a comment from coveralls Jun 16, 2017
Repository owner deleted a comment from coveralls Jun 16, 2017
Repository owner deleted a comment from coveralls Jun 16, 2017
Repository owner deleted a comment from coveralls Jun 16, 2017
Repository owner deleted a comment from coveralls Jun 16, 2017
@hernot
Copy link
Author

hernot commented Jun 17, 2017 via email

@tbeu
Copy link
Owner

tbeu commented Aug 26, 2017

I now took your test_class example (mat.zip), however I cannot see that matdump dumps the class object. Instead, when debugging it, I see that class_type is MAT_C_UINT8 for class_object or memmap_object (and MAT_C_OBJECT for inline_object as before). I believe this issue is related to #47 (MCOS). Can you please debug and check the expected behavior. Thanks.

@hernot
Copy link
Author

hernot commented Aug 26, 2017 via email

@tbeu
Copy link
Owner

tbeu commented Aug 26, 2017

You can take the MAT file from mat.zip or mat.zip as test data. I checked it on your plain commit , thus you could debug it with your local branch (no need to sync right now).

@hernot
Copy link
Author

hernot commented Aug 27, 2017

Ok checking the matlab documentation of memmapedfile i fear you can not memmap objects

From the documentation of the memmaped file Format parameter you can only specify numerical types no cells no char no struct and no object.

https://de.mathworks.com/help/matlab/ref/memmapfile.html

Thus could you please check (i do not have a matlab here,*) matdump reports the proper type for the following examples

char_object = 'Hello world'

memmap_object = memmapfile('c:\temp\mem_struct.map');
save object_uncompressed -v6 char_object memmap_object

struct_object=struct('Hello',{42},'World',{8*6});

memmap_object = memmapfile('c:\temp\mem_struct.map');
save object_uncompressed -v6 struct_object memmap_object

------ TestClass.m ------

classdef TestClass

properties (access = public)
a=1;
b="Hello World"
c
end
methods (access = public)
function obj = TestClass(surprise)
c = surprise;
end
end
end

object_object = TestClass('So long. Goodby and thanks for all the fish');
memmap_object = memmapfile('c:\temp\mem_object.map');
save object_uncompressed -v6 struct_object memmap_object

(*) or can you please at least prepare the saved files for me so that i can use them for debuging

What does matdump report in both cases. If the explicit format specifier does not allow any thing else than numeric types, no char, no cell, no struct, no object, no complexl, than i do suspect that char, cell, struct and object are not supported either when no format is specified.

What does matlab say when you try to reopen the above examples with memmappedfile for reading and what when you use normal load?
Do you get the char, struct and object back?

Which i fear is why they only show examples of numeric arrays and matrices to be mapped and no other data type.

Or matlab does some trick to store object as huge mat_c_uint8 type blob which it backconverts lateron. For classes it mandatorily would need the cassdef on the matlab path. To check and verify this hypothesis i would need at least the resulting saved files (memmap and normal).

@tbeu
Copy link
Owner

tbeu commented Nov 15, 2017

I get the same results as #64 (comment) if I export the MAT-files in R2014a instead of R2015b.

MAT files: mat.zip

@hernot
Copy link
Author

hernot commented Nov 16, 2017 via email

@tbeu
Copy link
Owner

tbeu commented Nov 16, 2017

Seems as if Matlab stores old style classes/objects defined via class directories @classname differentenly than newstyle classes defined via classdef. The ones we used in the old company were old style objects. Anyway.

Should I rather try to save old style object?

@hernot
Copy link
Author

hernot commented Nov 16, 2017 via email

@tbeu
Copy link
Owner

tbeu commented Feb 3, 2018

Is this still being worked on? If not, I propose to close it w/o merge?

@hernot
Copy link
Author

hernot commented Feb 4, 2018 via email

@tbeu
Copy link
Owner

tbeu commented Jun 13, 2018

One year later: can we close it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants