1
  • I have a field of type <1x1 java.lang.String> in Matlab. Its value is 13:06:40

  • When I read this mat-file in python, It gets converted to

MatlabOpaque([ ('', 'java', 'java.lang.String', [[172, 237, 0, 5, 116, 0, 8, 49, 50, 58, 48, 49, 58, 53, 49]])], 
      dtype=[('s0', 'O'), ('s1', 'O'), ('s2', 'O'), ('arr', 'O')])
  • I save this back to MAT-file using scipy.io and I have a struct with the dtype above instead of <1x1 java.lang.String>

Any way I can retrieve the time stamp using python and save it as a java.lang.String object ?

0

2 Answers 2

2

Access .mat file containing matlab classes in python

asks about a MATLAB class object with a similar loadmat display:

MatlabOpaque([ (b'futureDS', b'MCOS', b'cStream', [[3707764736], ...])], 
  dtype=[('s0', 'O'), ('s1', 'O'), ('s2', 'O'), ('arr', 'O')])

There's nothing in Python that can decode this kind of MATLAB or Java object. If you need to send data back and forth between MATLAB and scipy stick with the basic MATLAB arrays, cells, and struct.

You could try to parse that arr list of numbers. Since they are all <256, they probably represent bytes. Can't you convert it to an ordinary MATLAB character string?

In [117]: x=[172, 237, 0, 5, 116, 0, 8, 49, 50, 58, 48, 49, 58, 53, 49]
In [118]: np.array(x,np.uint8).tostring()
Out[118]: b'\xac\xed\x00\x05t\x00\x0812:01:51'

The last 8 characters look like a time stamp. But do you know anything about java.lang.String objects?

1
  • java.lang.String objects - not much. I scouted around the java docs but didn't find any information helpful to parse the message in python as such. But matlab has inbuilt java.lang.String support. So it looks like I have to write two scripts to accomplish my goal. Process files in python to do what I want and then post-process for date using Matlab.
    – Raaj
    Commented Oct 12, 2015 at 18:38
2

Probably not the answer you (or anybody else coming to this page) want to hear, but there doesn't seem to be an easy solution to this. As https://nbviewer.jupyter.org/gist/mbauman/9121961 points out, Matlab class objects stored in .mat files are an undocumented scheme. The above link gives some clues how to reverse engineer it if you are motivated - for me I'd rather go back to the Matlab file and save the date in a different format that is readable with scipy.io.loadmat().

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.