当前位置:网站首页>matlab数据类型 —— 字符型
matlab数据类型 —— 字符型
2022-06-26 23:42:00 【繁依Fanyi】
matlab系列文章:目录

〇、概述
对于数学来说,字符型数据可能不是很重要,但是用到的时候又必不可少。因此便有了本篇关于字符型数据的介绍。
在这里,字符型数据被分成了 字符串 和 字符变量 来介绍,两者用法稍有不同。
字符:和 Python、Java 等高级语言类型类似,专注与数学计算的 matlab 中也有字符型数据,就像是 a、b 等等只有一个字符的数据。
字符串:简单来说,字符串就是由若干个字符组合起来的数据,比如 ab、adccsa 等。
一、 字符与字符串的创建
1. 字符的创建
(1) 字符的创建
① 用单引号创建
>> a = 'abcd'
a =
'abcd'
>>
>> whos
Name Size Bytes Class Attributes
a 1x4 8 char
② 用 char 函数创建
>> b = char('abcasd')
b =
'abcasd'
>>
>> whos
Name Size Bytes Class Attributes
b 1x6 12 char
(2) 字符数组的创建
① 字符数组用 char 函数创建
字符数组同样用 char() 函数创建。
>> a1 = char('abcasd','asdass')
a1 =
2×6 char 数组
'abcasd'
'asdass'
>>
>> whos
Name Size Bytes Class Attributes
a1 2x6 24 char
2. 字符串的创建
(1) 字符串的创建
① 用双引号创建
>> c = "hello" % " 创建字符串
c =
"hello"
>>
>> whos
Name Size Bytes Class Attributes
c 1x1 150 string
② 用 string 函数创建
>> d = "hello world" % string() 函数创建字符串
d =
"hello world"
>>
>> whos
Name Size Bytes Class Attributes
d 1x1 166 string
(2) 字符数组的创建
① 字符数组用 [] 创建
>> c1 = ["hello" "world";"nihao" "hhh"] %注意,不同维度的向量用 ; 隔开
c1 =
2×2 string 数组
"hello" "world"
"nihao" "hhh"
>>
>> whos
Name Size Bytes Class Attributes
c1 2x2 312 string
二、字符 与 字符串 的相关操作
虽说是讲述 字符(char) 与 字符串(string) 的相关操作,但相较于 字符串(string)来说,字符(char)的使用更多,所以这里说字符(char)更多些。
1. 字符串的长度
常用的检测字符串长度的函数有两个 —— length() 与 size(),但是主要针对多个纬度的情况略有不同。
length()是从多个维度中选择最大值并返回size()是将多个维度中的长度以一个向量的形式返回
① length 函数
>> a = 'abcdef'
a =
'abcdef'
>>
>> length(a)
ans =
6
② size 函数
>> a = 'abcdef'
a =
'abcdef'
>>
>> size(a)
ans =
1 6
2. 字符串拼接
字符(char) 与 字符串(string) 的拼接方式稍有不同。
- 字符(
char) 通过strcat()函数进行拼接 - 字符串(
string) 直接通过+即可
① 字符串的拼接
字符(char) 通过 strcat() 函数进行拼接。
>> x = 'abcde'
x =
'abcde'
>>
>> y = 'fghij'
y =
'fghij'
>>
>> z = strcat(x,y)
z =
'abcdefghij'
② 字符串的拼接
字符串(string) 直接通过 + 即可。
>> a = "abc"
a =
"abc"
>>
>> b = "def"
b =
"def"
>>
>> c = a + b
c =
"abcdef"
3. 字符串的比较
① 字符串的比较
通过 strcmp() 函数可以比较两个 字符 或 字符串,当两个字符完全相同时,返回值为 1;若不同,则返回值为 0。
- 格式:
strcmp(s1,s2),s1 与 s2 为两个需要匹配的字符串。
>> a = 'abcd'
a =
'abcd'
>>
>> b = 'abcd'
b =
'abcd'
>>
>> strcmp(a,b)
ans =
logical
1
4. 查找字符串
① 查找字符串
如果我们想要查找某个长字符串里的短字符串,就可以使用 findstr() 函数来查找。
findstr()函数将会查找某个字符串中的子字符串,找到则将以数组的形式返回每个子串在长串中的起始位置。若找不到则将返回一个空数组。- 格式:
findstr(s_long,s_short),s_long为长字符串,s_short为短字符串(子串)
>> a = 'I am a good boy'
a =
'I am a good boy'
>>
>> findstr(a,'o')
ans =
9 10 14
5. 显示字符串
disp()是一个用来显示字符串的函数,就类似与 Java、Python 语言中的print()
>> a = 'I am a good boy'
a =
'I am a good boy'
>>
>> disp(a)
I am a good boy
6. 字符的索引
字符的索引也算是一个比较重要的功能:字符可以被索引,而字符串不能
① 索引字符
>> a = 'abcdef' %%字符
a =
'abcdef'
>>
>> for i = 1:3 %%索引字符
a(i)
end
ans =
'a'
ans =
'b'
ans =
'c'
② 索引字符串
>> b = "abcdef" %%字符串
b =
"abcdef"
>>
>> for i = 1:3 %%索引字符串
b(i)
end
ans =
"abcdef"
Index exceeds the number of array elements. Index must not exceed 1.
索引字符串会出现异常。

边栏推荐
- Service discovery, storage engine and static website of go language
- 简单测试轻量级表达式计算器Flee
- Technical dry goods | what is a big model? Oversized model? Foundation Model?
- 代码之外:写作是倒逼成长的最佳方式
- go语言的爬虫和中间件
- 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
- 深度学习方法求解平均场博弈论问题
- [microservices] Understanding microservices
- PHP代码审计系列(一) 基础:方法、思路、流程
- 互联网行业,常见含金量高的证书,看看你有几个?
猜你喜欢

Operations research says that in issue 66, Behrman also has "speech phobia"?

全網最全的混合精度訓練原理

PHP代码审计系列(一) 基础:方法、思路、流程

Simulation of delta variant strain of novel coronavirus (mindsponge application)

Microservices and container choreography in go

Mindspire, a domestic framework, cooperates with Shanshui nature conservation center to find and protect the treasure life in the "China water tower"

Thesis study -- Analysis of the influence of rainfall field division method on rainfall control rate

Your connection is not private

巧记大小端字节序

让敏捷回归本源——读《敏捷整洁之道》有感
随机推荐
My advanced learning notes of C language ----- keywords
Unity初学者肯定能用得上的50个小技巧
How to use Pinia (I) introduce Pinia into the project
泰国安全又划算的支付方式
Implement the queue through two stacks
Alibaba cloud server purchase, basic configuration, (xshell) remote connection and environment building
客户端实现client.go客户端类型定义连接
用户在hander()goroutine,添加定时器功能,超时则强踢出
如何通俗易懂的描述机器学习的流程?
How to open an account on the mobile phone? Is it safe to open an account online and speculate in stocks
Is it safe to open an account and speculate in stocks on the mobile phone? Is it safe to open an account and speculate in stocks on the Internet
Lorsque le transformateur rencontre l'équation différentielle partielle
test
Simulation of delta variant strain of novel coronavirus (mindsponge application)
PHP code audit series (I) basis: methods, ideas and processes
手机网上开户炒股安全吗 网上开户炒股安全吗
com.fasterxml.jackson.databind.exc.MismatchedInputException: Expected array or string. at [Source:x
Encapsulate servlet unified processing request
Is it reliable to open an account on a stock trading mobile phone? Is it safe to open an account online and speculate in stocks
Microservices and container choreography in go