MATLAB: Use string or character arrays in code

charstring

I am writing a few functions and scripts performing operations on files and content from files.
Often I use sprintf, uigetfile or other character/string based basic functions (even a simple isa is an example) that needs an input to be either a string or a character (sometimes even a cellstr is a good way of doing it).
Years ago when I was doing similar stuff in previous MATLAB release everything was handled in char arrays (with a little help from a cell when needed).
Nowadays it seems there's a new class in town – namely the string array.
Which is preferred and why? As far as I can tell the char is quite superior in memory usage, but are there any other advantages to using one over the other?

Best Answer

"Which is preferred and why?"
In my opinion character arrays and string arrays have quite different applications, even though people often seem to treat them as being interchangeable. In a nutshell, I would decide which one to use based on these paradigms:
  • string array: use when you want to consider each string element as an atomic unit (of course it is possible to dig into the string and mess around with its individual characters, but that is not their raison d'ĂȘtre). For example, strings allow for arrays with identical sizes to corresponding numeric arrays (e.g. simpler assigning of function outputs in a for-loop). The various special methods (e.g. compose, etc.) are sometimes very convenient but not particularly efficient.
  • character array: use when you need to work with individual characters, e.g. compression, encoding/decoding, or when you need to write very efficient code. With character arrays you can use standard MATLAB indexing and arithmetic operations etc. to work with individual character codes. There is no way to beat this in terms of speed or memory.