当前位置:网站首页>MATLAB data type - character type
MATLAB data type - character type
2022-06-27 00:26:00 【Fanyi】
matlab Series articles : Catalog

〇、 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 a、b Data with only one character .
character string : Simply speaking , A string is data composed of several characters , such as ab、adccsa 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 returnsize()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) adoptstrcat()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_longIs a long string ,s_shortIs 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 languageprint()
>> 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 .

边栏推荐
- From bitmap to bloom filter, C # implementation
- Can't write to avoid killing and can easily go online CS through defender
- [微服务]Nacos
- Is it safe to buy pension insurance online? Is there a policy?
- My advanced learning notes of C language ----- keywords
- CPU的异常处理
- Is it safe to open an account on your mobile phone to buy stocks? Is it safe to open an account online to speculate in stocks
- 敲重点!最全大模型训练合集!
- 股票怎样在手机上开户安全吗 网上开户炒股安全吗
- Alibaba cloud server purchase, basic configuration, (xshell) remote connection and environment building
猜你喜欢

剑指 Offer 10- II. 青蛙跳台阶问题

Sword finger offer 10- ii Frog jumping on steps

基于SSMP的宠物医院管理系统

墨者学院-X-Forwarded-For注入漏洞实战

Target tracking shooting? Target occlusion shooting? With 1.9 billion installed petal apps, what unique features attract users?

Le principe le plus complet de formation à la précision hybride pour l'ensemble du réseau

Introduction to message queuing

Let agile return to its original source -- Some Thoughts on reading the way of agile neatness

技术干货|极速、极智、极简的昇思MindSpore Lite:助力华为Watch更加智能

复杂数据没头绪?
随机推荐
Introduction to software engineering -- Chapter 4 -- formal description technology
JS library for number formatting
“message“:“Bad capabilities. Specify either app or appTopLevelWindow to create a session“
技术干货|极速、极智、极简的昇思MindSpore Lite:助力华为Watch更加智能
冲刺强基计划数学物理专题二
Leetcode 718. Longest repeating subarray (violence enumeration, to be solved)
[UVM actual battle== > episode_3] ~ assertion, sequence, property
[微服務]認識微服務
Your connection is not private
墨者学院-X-Forwarded-For注入漏洞实战
论文学习——降雨场次划分方法对降雨控制率的影响分析
深度学习方法求解平均场博弈论问题
邮箱附件钓鱼常用技法
“message“:“Bad capabilities. Specify either app or appTopLevelWindow to create a session“
com.fasterxml.jackson.databind.exc.MismatchedInputException: Expected array or string. at [Source:x
论文解读(LG2AR)《Learning Graph Augmentations to Learn Graph Representations》
test
find_circ详细使用指南
Common techniques of email attachment phishing
Competition Registration | one of the key ai+ scientific computing competitions - China open source scientific software creativity competition, competing for 100000 bonus!