Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between strings and characters in Matlab?

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

a = 'AX'; % This is a character.
b = string(a) % This is a string.
like image 574
Eghbal Avatar asked Feb 08 '17 17:02

Eghbal


People also ask

What is the difference between string and character?

The main difference between Character and String is that Character refers to a single letter, number, space, punctuation mark or a symbol that can be represented using a computer while String refers to a set of characters. In brief, String is a collection of characters.

What is a string in MATLAB?

You can represent text in MATLAB® using string arrays. Each element of a string array stores a sequence of characters. The sequences can have different lengths without padding, such as "yes" and "no" . A string array that has only one element is also called a string scalar.

What is the difference between strings and character arrays?

String refers to a sequence of characters represented as a single data type. Character Array is a sequential collection of data type char. Strings are immutable.

How do you define a character in MATLAB?

C = char( A ) converts the input array, A , to a character array. For instance, if A is a string, "foo" , c is a character array, 'foo' . C = char(A1,...,An) converts the arrays A1,...,An into a single character array. After conversion to characters, the input arrays become rows in C .


4 Answers

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 use cell 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. strings have overhead:

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

    returns

    Name      Size            Bytes  Class     Attributes
    
     a         1x3                 6  char                
     b         1x1               132  string
    
like image 147
4 revs, 2 users 98% Avatar answered Oct 07 '22 16:10

4 revs, 2 users 98%


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 versus string. 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 as 1x1 (and takes up more bytes in memory) while the character array is, as its name implies, a 1x2 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
    
like image 26
gnovice Avatar answered Oct 07 '22 16:10

gnovice


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.

string class

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
like image 5
Gelliant Avatar answered Oct 07 '22 17:10

Gelliant


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"
like image 4
v.tralala Avatar answered Oct 07 '22 16:10

v.tralala