returning string array from mex

16 views (last 30 days)
Brian
Brian on 16 Mar 2018
Commented: Brian on 16 Mar 2018
String arrays *often* have a more conservative memory footprint than cell and char arrays containing the same data, e.g.
>> cellArray = {'oneString'; 'anotherString'; 'moreSetring'}
cellArray =
3×1 cell array
'oneString'
'anotherString'
'moreSetring'
>> stringArray = string(cellArray)
stringArray =
3×1 string array
"oneString"
"anotherString"
"moreSetring"
>> whos
Name Size Bytes Class Attributes
cellArray 3x1 402 cell
stringArray 3x1 210 string
I'm still learning my way around the mex functions, and can't figure out how to return a string array from mex, whereas the case of returning a cell array is more obvious to me. I've tried the MATLAB demo mex mxcreatecharmatrixfromstr, but this returns a char array. At first it looked promising, with the same data set the char array consumed a lot less memory than either the cell or string arrays:
>> whos
Name Size Bytes Class Attributes
cellArray 3x1 402 cell
charArray 3x13 78 char
stringArray 3x1 210 string
But since char arrays have to be symmetric, this advantage quickly disappears when considering the cases when each elemental string has vastly different lengths. For example, with this test set of three strings:
'oneString', 'anotherStringaanotherStringaanotherStringaanotherStringaanotherStringaanotherStringaanotherStringaanotherStringa', 'moreSetring'
All three approaches show that string arrays have the most parsimonious memory consumption:
>> whos
Name Size Bytes Class Attributes
cellArray 3x1 600 cell
charArray 3x112 672 char
stringArray 3x1 408 string
So...
How can one construct a string array from within a mex and return it to the MATLAB workspace as such? In particular, I'm most interested in knowing how to construct a string array of a known number of elements but each with different lengths. For example, I know ahead of time how many strings I will have to store. However, the length of each string is only known as I'm iterating through incoming data.
Thanks!
  1 Comment
Brian
Brian on 16 Mar 2018
Sorry, quick correction on something I said. "But since char arrays have to be symmetric" not exactly what I intended to say. I had meant to say that all rows of a char array have memory allocated to all store the same number of columns as the row with the longest string. So all strings take up as much memory as the largest string irrespective of each of their own respective lengths.

Sign in to comment.

Accepted Answer

James Tursa
James Tursa on 16 Mar 2018
Edited: James Tursa on 16 Mar 2018
String arrays are a type of classdef object. They don't have any public properties that you can get at, so at present there is NO WAY TO GET AT OR SET the associated string data from within a mex function. Unless TMW chooses to provide such functionality in their API library, you are out of luck. I don't even know of a low level hack that can get at or set this data.
Char arrays and cell arrays of char arrays of course have plenty of mex API library support. So if you don't care about efficiency in building the string object, you could always build your char or cell array inside the mex routine and then call mexCallMATLAB with "string" as the function argument and convert it to a string object. Of course, that will essentially leave you with two copies of the data, but at present calling back to MATLAB is the only way to create a string object in a mex routine that I know of.
Similar comment for extracting the string data inside a mex routine ... you would have to call back to MATLAB using the "char" function to do it (leaving you with two copies again of course).
  1 Comment
Brian
Brian on 16 Mar 2018
Thanks, James for the answer and clear explanation as to why it is like it is, as well as some alternative possible workarounds.

Sign in to comment.

More Answers (0)

Categories

Find more on Characters and Strings 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!