MATLAB String Array

Source: Internet
Author: User

4.1 String Array

4.1.1 getting started with strings

[* Example 4.1.1-1] First, ask the reader to perform actual operations in this example to see the difference between the numeric value and the string.
Clear % clear all memory Variables
A = 12345.6789% assign a numeric scalar to variable
Class (a) % determines the class of variable
A_s = size (a) % value array A's "size"
A =
1.22.16e + 004
Ans =
Double
A_s =
1 1

B ='s '% assigned variable B a character scalar (that is, a single character)
Class (B) % determines the type of variable B
B _s = size (B) % Represents the "size" of array B"
B =
S
Ans =
Char
B _s =
1 1
Whos % observe the bytes occupied by variables A and B in the memory
Name size bytes class
A 1x1 8 double Array
A_s 1X2 16 double Array
Ans 1X4 8 char array
B 1x1 2 char array
B _s 1X2 16 double Array
Grand total is 10 elements using 50 bytes

4.1.2 attributes and identifiers of string Arrays

[* Example 4.1.2-1] This example shows the basic attributes, identifiers, and simple operations of a string.

(1) create a String Array
A = 'this is an example .'
A =
This is an example.

(2) Size of String Array
Size ()
Ans =
1 19

(3) element ID of the string array
A14 = a () % raised a substring
Ra = a (end:-1:1) % inverted string
A14 =
This
Ra =
. Elpmaxe na Si siht

(4) ASCII code of the string array
Ascii_a = double (a) % generates ASCII code
Ascii_a =
Columns 1 through 12
84 104 105 115 32 105 115 32 97 110 32 101
Columns 13 through 19
120 97 109 112 108 46
Char (ascii_a) % returns the ASCII code back to the string
Ans =
This is an example.

(5) operations on the string ASCII code Array
% Uppercase letters in the string
W = find (A> = 'A' & A <= 'Z'); % locate the element location of lowercase letters in string array.
Ascii_a (w) = ascii_a (w)-32; % ASCII value difference between uppercase and lowercase letters 32. Use the value addition to change part of the code value.
Char (ascii_a) % converts the new ASCII code into characters
Ans =
This is an example.

(6) Chinese String Array
A = 'This is an example. '; % Create a Chinese String
A_s = size (a) % size of the string array
A56 = a ([5 6]) % obtain the sub-array of the string
Ascii_a = double (a) % get ASCII code
A_s =
1 7
A56 =

Example
Ascii_a =
Columns 1 through 6
54754 51911 53947 47350 52195 49405
Column 7
41379

Char (ascii_a) % translates ASCII code into characters
Ans =
This is an example.

(7) create a string with single quotes
B = 'example ''' 4.1.2-1 '''
B =
Example '4.1.2-1'

(8) A long string composed of small strings
AB = [A (), '', B, '.'] % here, the 2nd inputs are space strings.
AB =
This is example '4.1.2-1 '.

4.1.3 create a complex String Array

4.1.3.1 direct creation of multi-row string Arrays

[* Example 4.1.3. 1-1] direct input example of multi-line string array.
Clear
S = ['this string array'
'Has multiple rows. ']
S =
This string array
Has multiple rows.
Size (s)
Ans =
18

4.1.3.2 create multi-row string arrays using string operation functions

[* Example 4.1.3. 2-1] example: use special functions char, str2mat, and strvcat to create multi-row string array.
S1 = char ('this string array', 'has two rows .')
S1 =
This string array
Has two rows.
S2 = str2mat ('data', 'char', 'string' array, 'consists of 4 rows ')
S2 =
This
Character
String Array
Composed of four rows
S3 = strvcat
S3 =
This
Character
String Array
Composed of four rows
Size (S3)
Ans =
5 5

Supplement of [* example 4.1.3. 2-1]

(1) create a two-dimensional character array animal

> Animal = ['Dog'; 'monkey'];

??? Error Using ==> vertcat

Cat arguments dimensions are not consistent.

> Animal = ['Dog'; 'monkey']; % created successfully

Note: When creating a two-dimensional character array, each character array must contain the same column. When a multi-line string has different lengths, to avoid errors, you must add spaces to a short string to ensure the length of the short string and the longest string.

(2) create a character array using the char function. This method does not require the length of all strings.

> Animal = char ('dog ', 'monkey ');

4.1.3.3 conversion functions generate digital strings

[* Example 4.1.3. 3-1] int2str, num2str, and mat2str are the most common array/string conversion functions.

(1) int2str converts an integer array into a String Array (non-integers are rounded to the garden before conversion)
A = eye (2, 4); % generates a numerical array
A_str1 = int2str (a) % is converted into a string array. Please use the size test on your own.
A_str1 =
1 0 0 0
0 1 0 0

(2) num2str converts a non-integer array to a string array (commonly used in graphics, data point identification)
Rand ('state', 0)
B = rand (2, 4); % to generate a numerical Matrix
B3 = num2str (B, 3) % keep three valid digits and convert them to strings
B3 =
0.95 0.607 0.891 0.456
0.231 0.486 0.762 0.0185

(3) mat2str converts a numeric array into a string array in the input form (often with EVAl command configuration)
B _str = mat2str (B, 4) % to keep four valid digits and convert them to "array input form" strings
B _str =
[0.9501 0.6068 0.8913 0.4565; 0.2311 0.486 0.7621 0.0185]
ExprEssion = ['exp (-', B _str,') ']; % is equivalent to writing an expression exp (-B _str) in the Command window)
EVAl (exprEssion) % send exp (-B _str) to execute
Ans =
0.3867 0.5451 0.4101 0.6335
0.7937 0.6151 0.4667 0.9817

[* Example 4.1.3. 3-2] A comprehensive example is as follows: the plot name and the maximum coordinate are displayed on the graph generated by Matlab.
Clear % clear all variables in memory
A = 2; % set the attenuation coefficient
W = 3; % set the oscillation frequency
T = 0: 0. 0; % retrieved from the Variable Sampling Array
Y = exp (-A * t). * sin (w * t); % calculate the function value and generate the Function Array
[Y_max, I _max] = max (y); % locate the maximum element position
T_text = ['t =', num2str (T (I _max)]; % generate the x-axis string of the maximum vertex <7>
Y_text = ['y = ', num2str (y_max)]; % generate the ordinate string of the maximum vertex <8>
Max_text = char ('maximum ', t_text, y_text); % generate the string that marks the maximum value <9>
% String used to generate the logo image name
Tit = ['y = exp (-', num2str (A),'t) * sin (', num2str (W),'t) ']; % <11>
Plot (T, zeros (SIZE (t), 'k') % draw the baseline with the ordinate value 0
Hold on % keep drawn lines not cleared
Plot (T, Y, 'B') % plot Y (t) curve with blue
Plot (T (I _max), y_max, 'R. ', 'markersize', 20) % use a large red dot to mark the maximum Vertex
Text (T (I _max) + 0.3, y_max + 0.05, max_text) % write the data value of the maximum Vertex on the graph <16>
Title (tit), xlabel ('T'), ylabel ('y'), hold off % write image name, abscissa name, and ordinate name

Figure 4.1.3. 3-1 string usage

4.1.3.4 use the cellular array to create a complex string

[* Example 4.1.3. 4-1] The application of the cellular array in the storage and operation strings.
A = 'matlab 5'; B = 'introduces new DATa types: '; % create a single line string a, B
C1 = '◆ multimen=array'; C2 = '◆ user-definable daTa structure ';
C3 = '◆ cell arrays'; C4 = '◆ character array ';
C = char (C1, C2, C3, C4); % create multi-line string C
C = {A; B; C };%use the cellular array to store strings of different lengths <5>
Disp ([c {}]) % displays the characters in the first two cells <6>
Disp ('') % displays a blank line
Disp (c {3}) % displays the characters in the 3rd meta cells <8>
MATLAB 5 introduces new DATa types:
◆ Multidimen1_array
◆ User-definable daTa Structure
◆ Cell arrays
◆ Character array

4.1.4 String Conversion Function

[* Example 4.1.4-1] usage example of fprintf, sprintf, and sscanf.
Rand ('state', 0); A = rand (2, 2); % generates random arrays
S1 = num2str (a) % converts a numeric array to a String Array
S_s = sprintf ('%. 10E/N', A) % 10 digital scientific description string. Each element is written with a line feed.
S1 =
0.95013 0.60684
0.23114 0.48598
S_s =
9.5012928515e-001
2.3112131357e-001
6.0682138354e-001
4.8592136871e-001

Fprintf ('%. 5g //', a) % is displayed in the shortest form of 5 digits. Values cannot be assigned.
0.95013/0.23114/0.60684/0.48598/

S_sscan = sscanf (s_s, '% F', [3, 2]) % convert the string to a numerical array in floating-point format.
S_sscan =
0.9501 0.4860
0.2311 0

0.6068 0

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.