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!
Best Answer