mxAddField/mxGetField with unicode name

2 views (last 30 days)
I am trying to write code that allows for arbitrary field names in Matlab structures. I thought I had a decent solution, based on using mex to set invalid fields. To make the interface the same as a normal structure, I overloaded subsref in an object so that the mex call is made transparently. Retrieval of invalid fields is possible using dynamic structure indexing (e.g. value = s.("invalid name")).
Everything seemed to be working well, then I realized that there is no public mex interface to set fieldnames using the 2-byte per character string format that Matlab uses. This means I am unable to set a field with a non-ASCII fieldname. Instead it seems like struct fields may instead be stored as c-style strings in ASCII format. Is this true? Is it possible to work with structures using 2-byte wide characters. An alternative is to wrap everything in the object or to use something like containers.Map, but both are much slower to use than structures, and I'd like to be able to fall back to structures (from objects) in certain use cases when the setting of additional invalid fields using the "set field" mex function isn't necessary. Any other suggestions?

Accepted Answer

James Tursa
James Tursa on 25 Nov 2016
Edited: James Tursa on 25 Nov 2016
... Is it possible to work with structures using 2-byte wide characters? ..."
If I understand your question, the answer is NO. Field names in mxArray struct variables are stored as 1-byte per character strings. You are stuck with that. Where these strings are stored in the mxArray has changed over the years (e.g., info behind the pi pointer), but it has always been 1-byte per character as far as I know (back to R2006a at least).
You could, of course, hack into those strings to set them to whatever you wanted (e.g., overwrite them with 2-byte per character strings), but that would mess up anything at the MATLAB level that tried to use those strings. E.g., I doubt you would be able to get dynamic field name access to work. So all of that access (and even just displaying the 2-byte per character field names) would likely have to be done inside a mex routine. I.e., just use the mxGetFieldNameByNumber API routine to get a pointer to the field name string and write over that string (keeping the total number of characters constant).
  1 Comment
Jim Hokanson
Jim Hokanson on 25 Nov 2016
Yeah, I briefly considered building some hack around the 1 byte limitation, but I thought things would start getting ugly very quickly.

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB Compiler in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!