当前位置:网站首页>MATLAB data type - character type

MATLAB data type - character type

2022-06-27 00:26:00 Fanyi

matlab Series articles Catalog

 Insert picture description here

〇、 summary

For Mathematics , Character data may not be very important , But it is necessary when it is used . Therefore, we have this introduction to character data .

ad locum , Character data is divided into character string and Character variables To introduce , The usage of the two is slightly different .

character : and Python、Java And other high-level language types , Focused on mathematical calculations matlab There are also character data in , like ab Data with only one character .

character string : Simply speaking , A string is data composed of several characters , such as abadccsa etc. .

One 、 Creation of characters and strings

1. Character creation

(1) Character creation

① Create with single quotation marks
>> a = 'abcd'

a =

    'abcd'

>>
>> whos
  Name      Size            Bytes  Class    Attributes

  a         1x4                 8  char               

② use char Function creation
>> b = char('abcasd')

b =

    'abcasd'

>>
>> whos
  Name      Size            Bytes  Class    Attributes

  b         1x6                12  char               

(2) Creation of character array

① The character array uses char Function creation

Character arrays also use char() Function creation .

>> a1 = char('abcasd','asdass')

a1 =

  2×6 char  Array 

    'abcasd'
    'asdass'

>>
>> whos
  Name      Size            Bytes  Class    Attributes
         
  a1        2x6                24  char               

2. String creation

(1) String creation

① Create... In double quotes
>> c = "hello"	% "  Create string 

c = 

    "hello"

>>
>> whos
  Name      Size            Bytes  Class     Attributes

  c         1x1               150  string              
② use string Function creation
>> d = "hello world"	% string()  Function to create a string 

d = 

    "hello world"

>>
>> whos
  Name      Size            Bytes  Class     Attributes

  d         1x1               166  string                

(2) Creation of character array

① The character array uses [] establish
>> c1 = ["hello" "world";"nihao" "hhh"]	% Be careful , Vectors of different dimensions are represented by  ;  separate 

c1 = 

  2×2 string  Array 

    "hello"    "world"
    "nihao"    "hhh"  

>>
>> whos
  Name      Size            Bytes  Class     Attributes

  c1        2x2               312  string              

Two 、 character And character string Related operations of

Although it is about character (char And character string (string Related operations of , But compared to character string (string) Come on , character (char) More use of , So here's the character (char) More .

1. Length of string

There are two commonly used functions to detect the length of a string —— length() And size(), But the situation is slightly different mainly for multiple latitudes .

  • length() Is to select the maximum value from multiple dimensions and return
  • size() Is to return the length in multiple dimensions as a vector

① length function

>> a = 'abcdef'

a =

    'abcdef'

>>
>> length(a)

ans =

     6

② size function

>> a = 'abcdef'

a =

    'abcdef'

>>
>> size(a)

ans =

     1     6

2. String splicing

character (char And character string (string The splicing method is slightly different .

  • character (char adopt strcat() Function
  • character string (string Directly through + that will do

① String splicing

character (char adopt strcat() Function .

>> x = 'abcde'

x =

    'abcde'

>>
>> y = 'fghij'

y =

    'fghij'

>>
>> z = strcat(x,y)

z =

    'abcdefghij'

② String splicing

character string (string Directly through + that will do .

>> a = "abc"

a = 

    "abc"

>>
>> b = "def"

b = 

    "def"

>>
>> c = a + b

c = 

    "abcdef"

3. String comparison

① String comparison

adopt strcmp() Function can compare two character or character string , When two characters are identical , The return value is 1; If different , The return value is 0.

  • Format :strcmp(s1,s2),s1 And s2 For two strings that need to be matched .
>> a = 'abcd'

a =

    'abcd'

>>
>> b = 'abcd'

b =

    'abcd'

>>
>> strcmp(a,b)

ans =

  logical

   1

4. Find string

① Find string

If we want to find a short string in a long string , You can use findstr() Function to find .

  • findstr() The function will find the substring in a string , If found, the starting position of each substring in the long string will be returned in the form of an array . If not found, an empty array will be returned .
  • Format :findstr(s_long,s_short),s_long Is a long string ,s_short Is a short string ( Substring )
>> a = 'I am a good boy'

a =

    'I am a good boy'

>> 
>> findstr(a,'o')

ans =

     9    10    14

5. display string

  • disp() Is a function used to display strings , It's similar to Java、Python In language print()
>> a = 'I am a good boy'

a =

    'I am a good boy'

>>
>> disp(a)
I am a good boy

6. Index of characters

Character indexing is also an important function : Characters can be indexed , The string cannot

① Index character

>> a = 'abcdef'		%% character 

a =

    'abcdef'

>>
>> for i = 1:3		%% Index character 
a(i)
end

ans =

    'a'


ans =

    'b'


ans =

    'c'

② Index string

>> b = "abcdef"		%% character string 

b = 

    "abcdef"

>>
>> for i = 1:3		%% Index string 
b(i)
end

ans = 

    "abcdef"

Index exceeds the number of array elements. Index must not exceed 1.

An exception occurs in the index string .

 Insert picture description here

原网站

版权声明
本文为[Fanyi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206262341560682.html