mrkrd / matlab_wrapper

Easy to use MATLAB wrapper for Python
GNU General Public License v3.0
78 stars 23 forks source link

chararrays are all mixed up #29

Open lrq3000 opened 5 years ago

lrq3000 commented 5 years ago

Matlab functions returning a chararray are not properly parsed by the wrapper, they get all mixed up (all characters of 1st column, then all characters from 2nd column etc. in a single string, whereas it should return a row-wise list).

Example:

mlab.eval('res = ls')
mlab.get('res')

Returns:

'..777AAABCCCCCDDFHHIIILLLMMMMMMMMMMMMMOOOOOOOOOOOPPPPPPPPPPPPQQQQQQQQQQQQQQQQQQQQQSSSSSSTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTWWXaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccddddddddddddddddddddddddddddddddddddddddddddddddddddddeeeeeeeeeeeefffffffffffffggggggggggggggggggggggggggggggggghhhhhhhhhhhhhhhhhhiiiiiiiiiiiiiiiiiiiiiiijjjjjjjjjjjjkkklllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll

Etc (this is an excerpt).

There is a way to workaround this by converting the output to a cellarray first using cellstr:

mlab.eval('res = cellstr(ls)')
mlab.get('res')
mrkrd commented 5 years ago

On using ls:

It looks that MATLAB's ls generates 2d char array on Windows only. On Linux it returns a char vector. For this reason, it's best to avoid using 'ls' or your risk compatibility problems on different platforms.

Generally, I would suggest using Python for file listing/manipulation and reduce MATLAB code to minimum, e.g., a single function with simple input/output.

On char arrays:

It looks like a bug. At the moment matlab_wrapper converts any char array to a string in Python. As a result is scrambles 2d MATLAB arrays. I could accept a patch for this case, but it's still not obvious what the corresponding data type on Python side should be:

lrq3000 commented 4 years ago

Hello @mrkrd , thank you for your reply. Unfortunately, I need to use a matlab script to get back strings, as I have no other way to generate them without rewriting a significant portion of complex code.

But I've found a workaround: simply convert a cellarray of strings into a char(), and then in Python use the .tolist() method to properly convert it to a list of strings, and that's working flawlessly. Maybe something could be added in the readme to clue others who may have a similar need?

lrq3000 commented 4 years ago

To be more accurate, what I did is that I have made a matlab auxiliary script that calls the real matlab function I need to get the strings I want, and then this auxiliary script returns a char(var) where var is a cellarray of the strings I need. Let's call this auxiliary matlab script "expandhelper.m".

I then call this script from Python using:

finallistofstrings = mlab.workspace.expandhelper(listofstrings).tolist()

And that's all, they are then properly formatted.

lrq3000 commented 4 years ago

Update and clarification: it's a code I wrote a long time ago, I forgot some of the details. In fact, in the matlab auxiliary script, it's returning a cellstr(char(var)), so a cellstr array of the variable wrapped inside a char. So in the end, what works is returning a cellstr array, not a char array.