What is the difference between strings and characters in Matlab?
One important practical thing to note is, that strings and chars behave differently when interacting with square brackets. This can be especially confusing when coming from python. consider following example:
>>['asdf' '123']
ans =
'asdf123'
>> ["asdf" "123"]
ans =
1×2 string array
"asdf" "123"
Strings do have a bit of overhead, but still increase by 2 bytes per character. After every 8 characters it increases the size of the variable. The red line is y=2x+127
.
figure is created using:
v=[];N=100;
for ct = 1:N
s=char(randi([0 255],[1,ct]));
s=string(s);
a=whos('s');v(ct)=a.bytes;
end
figure(1);clf
plot(v)
xlabel('# characters')
ylabel('# bytes')
p=polyfit(1:N,v,1);
hold on
plot([0,N],[127,2*N+127],'r')
hold off
The best place to start for understanding the difference is the documentation. The key difference, as stated there:
- A character array is a sequence of characters, just as a numeric array is a sequence of numbers. A typical use is to store short pieces of text as character vectors, such as
c = 'Hello World';
.- A string array is a container for pieces of text. String arrays provide a set of functions for working with text as data. To convert text to string arrays, use the
string
function.
Here are a few more key points about their differences:
- They are different classes (i.e. types):
char
versusstring
. As such they will have different sets of methods defined for each. Think about what sort of operations you want to do on your text, then choose the one that best supports those. Since a
string
is a container class, be mindful of how its size differs from an equivalent character array representation. Using your example:>> a = 'AX'; % This is a character. >> b = string(a) % This is a string. >> whos Name Size Bytes Class Attributes a 1x2 4 char b 1x1 134 string
Notice that the
string
container lists its size as1x1
(and takes up more bytes in memory) while the character array is, as its name implies, a1x2
array of characters.They can't always be used interchangeably, and you may need to convert between the two for certain operations. For example,
string
objects can't be used as dynamic field names for structure indexing:>> s = struct('a', 1); >> name = string('a'); >> s.(name) Argument to dynamic structure reference must evaluate to a valid field name. >> s.(char(name)) ans = 1
The documentation suggests:
There are two ways to represent text in MATLAB®. You can store text in character arrays. A typical use is to store short pieces of text as character vectors. And starting in Release 2016b, you can also store multiple pieces of text in string arrays. String arrays provide a set of functions for working with text as data.
This is how the two representations differ:
Element access. To represent
char
vectors of different length, one had to usecell
arrays, e.g.ch = {'a', 'ab', 'abc'}
. With strings, they can be created in actual arrays:str = [string('a'), string('ab'), string('abc')]
. However, to index characters in a string array directly, the curly bracket notation has to be used:str{3}(2) % == 'b'
Memory use. Chars use exactly two bytes per character.
string
s have overhead:a = 'abc' b = string('abc') whos a b
returns
Name Size Bytes Class Attributes a 1x3 6 char b 1x1 132 string