Conversion of character arrays and string arrays in MATLAB

In MATLAB, character arrays and string arrays are two concepts that are easily confused. If the understanding between character arrays and string arrays is not very clear, it is easy to cause mistakes.

For example, the following example:

A=['Java','Python','GO','PHP'];
B=["Java","Python","GO","PHP"];
A(2)
B(2)

The results displayed after running are as follows:

ans =
    'a'
ans = 
    "Python"

Through the running results, we can see that the character array displays the second character, while the string array displays the second string. There are obvious differences in the results of the two operations.

In MATLAB, it is often necessary to convert between character arrays and string arrays. For a character array, the character array will be converted to a string when it is converted to a string array, and the conversion method is: string (character array); for a string array, when the string array is converted to a character array First, split each string into characters and put them into an array. The conversion method is: char (string).

For example, to convert the above example:

A=['Java','Python','GO','PHP'];
B=["Java","Python","GO","PHP"];
A(2)
B(2)
C=string(A);
C(1)
D=char(B);
D(2)

The result after running is as follows:

ans =
    'a'
ans = 
    "Python"
ans = 
    "JavaPythonGOPHP"
ans =
    'a'

Guess you like

Origin blog.csdn.net/qq_54186956/article/details/126447130