Set and get structure field with invalid name
Show older comments
I'd like to get and set a structure with potentially invalid field names. Before digging into the mex myself I was wondering if there was already a tool that existed that performed these functions. I swear I've seen this around but my search skills are failing me. Finally, I assume this is possible with mex ....
Thanks, Jim
1 Comment
Jim Hokanson
on 28 Jul 2016
Accepted Answer
More Answers (2)
James Tursa
on 27 Jul 2016
Edited: James Tursa
on 27 Jul 2016
0 votes
I would start with this if you are trying to get stuff from a mat file:
Although I haven't looked at it in awhile to see if it still works for later versions of MATLAB.
If you are doing something else let me know and maybe we can customize it for your needs.
UPDATE:
I just did a quick check and the included savebadnames.c function does not work anymore. Apparently the matPutVariable API function now checks for invalid names. While this is probably a good thing, it also means I can't generate a bad mat file to use for checking the functionality of the main routine loadfixnames.c. I may have to write some custom C code to create the mat file from scratch without using the API functions. But that is not trivial, so it will not happen anytime soon ...
Hopefully the matGetNextVariable API function does not do this check. If it does then loadfixnames.c is toast ...
5 Comments
Jim Hokanson
on 28 Jul 2016
James Tursa
on 28 Jul 2016
Funny ... I thought you had a struct already with bad field names and needed to fix them ... not the other way around! That's where the mat file stuff comes in btw, because that is typically where these structs with bad field names come from. Good luck with Jan's code.
Jim Hokanson
on 28 Jul 2016
James Tursa
on 28 Jul 2016
Edited: James Tursa
on 28 Jul 2016
Some comments:
It sounds like you want to do the equivalent of the following with a mex routine:
Create a struct with arbitrary field names
Add arbitrary field names to an existing struct
Set an arbitrary field element to something, e.g.,
mystruct.('Bad Field Name$') = something
1) Creating a struct with arbitrary field names is easy in a mex routine, since the mxCreateStructArray API function apparently does not check for invalid field names, at least on the R2011b version I just spot checked. Not sure about later versions. If later versions do check for invalid field names, this becomes much more difficult and would probably require hacking into the mxArray. That in itself is very tricky because the method of storing field names in a mxArray is different depending on which version of MATLAB you are running.
2) Modifying an existing struct in a mex routine, either by adding a new field or by setting a field, has the problem of efficiency. The only way to take an input struct and modify it with official API functions is to first create a deep copy of it with mxDuplicateArray, and then do the modifications. If your field elements are large, then you get memory bloat and speed problems. The only way to avoid this is to use unofficial API functions to do what MATLAB does at the m-file level ... modify only the field element you want and make everything else a reference copy. This is memory efficient and fast, but you have no way to do this with official API functions. For a first cut at this, I would advise using the mxDuplicateArray approach just to get things working. Don't worry about the unofficial efficient approach unless you really really need it.
3) I haven't looked at Jan's code so I don't know what it does or how it does it. So I have no advice on using it. But writing the basic functionality described above in a mex routine would only take some 20-30 lines of code (plus argument checking). Let me know if you want me to post it or if you need more help.
Jim Hokanson
on 30 Jul 2016
Edited: Jim Hokanson
on 30 Jul 2016
Steven Lord
on 28 Jul 2016
Trying to fiddle around with invalid data is probably not going to be robust (or possible, if we've done our jobs well.)
Q = System.Collections.Specialized.OrderedDictionary();
Q.Add('123 for all!', '2734')
Q.Item('does not exist') % returns []
Q.Item('123 for all!') % returns System.String('2734')
char(Q.Item('123 for all!')) % returns the char array '2734'
I imagine there's probably something similar you could do in Java if you need your code to run on a non-Windows platform.
1 Comment
Jim Hokanson
on 30 Jul 2016
Categories
Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!