ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

MatLab---字符串,字符数组与文本操作常用函数

2022-04-25 21:02:29  阅读:142  来源: 互联网

标签:char scale Apple --- MatLab 数组 ans 文本 string


 

1.class():测试变量类型

Apple_char='Apple'
Apple_char =
'Apple'
>> class(Apple_char)
ans =
'char'
>> Apple_string="Appple"
Apple_string =
"Appple"
>> class(Apple_string)
ans =
'string'

2.强制类型转换成double

double(Apple_char)
ans =
65 112 112 108 101

行数和列数
>> size(Apple_char)
ans =
1 5

行数和列数,较大的值
>> length(Apple_char)
ans =
5

字符的转置
>> Apple_char'
ans =
5×1 char 数组
'A'
'p'
'p'
'l'

字符以数组的形式存储

Apple_char(2)
ans =
'p'
>> Apple_char(4)
ans =
'l'

>> size(Apple_string)
ans =
1 1
>> Apple_string
Apple_string =
"Appple"

char是数组,向量而string是一个标量
>> Apple_char% 数组/向量
Apple_char =
'Apple'
>> Apple_string%标量
Apple_string =
"Appple"
>> length(Apple_string)
ans =
1
>> Apple_string(1)
ans =
"Appple"

vector=[1 2 3 4]
vector =
1 2 3 4
>> vector(1)
ans =
1
>> scale=pi  %scale是个标量
scale =
3.1416
>> scale(2)
索引超出数组元素的数目(1)。

'scale' 似乎同时为函数和变量。如果这不是预期的情况,请使用 'clear scale' 将变量 'scale' 从工作区中删除。
>> scale(1)
ans =
3.1416

Apple_string{1}    %类型发生了改变,变成char
ans =
'Appple'
>> Apple_string{1}(2)
ans =
'p'
>> Apple_string{1}      %类型发生了改变,变成char
ans =
'Appple'

major_string=["English","History","Engineering"]
major_string =
1×3 string 数组
"English" "History" "Engineering"

major_char=['English','History','Engineering'] %[1:3 4:7 5:9]
major_char =
'EnglishHistoryEngineering'
>> major_char=char('English','History','Engineering')      %char的强制类型转换
major_char =
3×11 char 数组
'English '
'History '
'Engineering'
>> %注意用空格补齐,如果字符本身结尾处就有空格,就会出现问题

空值问题:

[' ',' ']
ans =
空的 0×0 char 数组
>> % " "还是标量
>> [" "," "]
ans =
1×2 string 数组
"" ""

空格表示

blanks(4)

ans =

'     '

repmat()的用法

>> repmat([1 2 3],4,3)

ans =

1 2 3 1 2 3 1 2 3
1 2 3 1 2 3 1 2 3
1 2 3 1 2 3 1 2 3
1 2 3 1 2 3 1 2 3

>> repmat('a',4,3)

ans =

4×3 char 数组

'aaa'
'aaa'
'aaa'
'aaa'

>> repmat('ab',4,3)

ans =

4×6 char 数组

'ababab'
'ababab'
'ababab'
'ababab'

方括号的组合功能:

>> ['Space' blanks(4) 'Cowboy']

ans =

'Space    Cowboy'   %将三个独立的字符串,合并到一起;

 



标签:char,scale,Apple,---,MatLab,数组,ans,文本,string
来源: https://www.cnblogs.com/lsj89/p/16189570.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有