What is the difference between strings and characters in matlab?

What is the difference between string and character classes in MATLAB?

a = 'AX'; % This is a character. b = string(a) % This is a string. 
+7
string char matlab
source share
3 answers

The documentation offers :

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. Starting from version 2016b, you can also store several pieces of text in string arrays. String arrays provide a set of functions for working with text as data.

So two views differ:

  • Access to the item . To represent char vectors of various lengths, cell arrays had to be used, for example. ch = {'a', 'ab', 'abc'} . Using strings, they can be created in real arrays: str = [string('a'), string('ab'), string('abc')] . However, for index characters in a string array directly, you must use curly braces:

     str{3}(2) % == 'b' 
  • Memory usage . Balls use as many bytes as characters. string have overhead:

     a = 'abc' b = string('abc') whos ab 

    returns

     Name Size Bytes Class Attributes a 1x3 6 char b 1x1 132 string 
+6
source share

The best place to understand the difference is documentation . The key difference, as indicated there:

  • A character array is a sequence of characters, just as a numeric array is a sequence of numbers. A typical use is to save short fragments of text as character vectors, for example c = 'Hello World'; .
  • A string array is a container for text fragments. 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 key points of their differences:

  • These are different classes (i.e. types): char compared to string . Thus, they will have different sets of methods defined for each. Think about what operations you want to do in your text, and then choose the one that best supports them.
  • Since a string is a container class, remember how its size differs from the equivalent representation of an array of characters. 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 

    Note that the string container lists its size as 1x1 (and takes up more bytes in memory), while the array of characters, as its name implies, represents an array of 1x2 characters.

  • They may not always be used interchangeably, and you may need to convert them between two operations. For example, string objects cannot be used as dynamic field names for indexing a structure :

     >> 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 
+3
source share

Strings have a bit of overhead, but still increase by 2 bytes per character. After every 8 characters, it increases the size of the variable. Red line y=2x+127 .

string class

A picture 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 
+2
source share

All Articles