matlab笔记

1、strcmp
是用于做字符串比较的函数,按复杂程度及比较对像的不同主要可以分为以下三种情况:
1. TF=strcmp(s1,s2);
    s1和s2是字符串,比如:s1=‘hello’,s2='matlab'。
    如果s1和s2是一致的(identical),则返回值TF=1,否则,TF=0。
    >> s1='hello';
    >> s2='hello';
    >> s3='matlab';
    >> TF1=strcmp(s1,s2);
    >> TF1
    TF1 =
         1
    >> TF2=strcmp(s1,s3);
    >> TF2
    TF2 =
         0

2. TF=strcmp(s,c);
    s是一个字符串,the c is a cell array,c的元素全是字符串,比如:s=‘hello’,c={'hello','matlab';'HELLO','matlab'}。
    返回值TF是一个和c有相同size的logical array,TF的元素是1或0。
    把c中的每个元素和s做比较,如果一致,则TF对应位置的元素为1,否则,为0。
    >> s='hello';
    >> c={'hello','matlab';'HELLO','matlab'};
    >> TF=strcmp(s,c);
    >> TF
    TF =
     1     0
     0     0

3. TF=strcmp(c1,c2);          
    c1和c2都是cell arrays,并且它们具有相同的size,它们的元素都是字符串,比如c1={'hello','matlab';'HELLO','matlab'};c2={'hello','matlab';'hello','MATLAB'};
    返回值TF是一个和c1或c2有相同size的logical array,TF的元素是1或0。
    把c1和c2对应位置的元素做比较,如果一致,则TF对应位置的元素为1,否则,为0。

    >> c1={'hello','matlab';'HELLO','matlab'};
    >> c2={'hello','matlab';'hello','MATLAB'};
    >> TF=strcmp(c1,c2);
    >> TF
    TF =
     1     1
     0     0
     
4. 扩展
strcmp(s1, s2):用于比较字符串s1、s2是否相等,如果相等,返回结果1,否则返回0;

strncmp(s1, s2, n):用于比较字符串s1、s2前n个字符是否相等,如果相等,返回结果1,否则返回0;

strcmpi(s1, s2):在忽略字母大小写的前提下,比较字符串s1、s2是否相等,如果相等,返回结果1,否则返回0;

strncmpi(s1, s2, n):在忽略字母大小写的前提下,比较字符串s1、s2前n个字符是否相等,如果相等,返回结果1,否则返回0。
————————————————

2 拼接:
2.1 矩阵合并拼接
2.1.1 横向拼接:[a,b] [a b]均可
2.1.2 纵向拼接:[a;b]

2.2 使用数组串联运算符[]串联元胞数组

>> a={1,2,3};
>> b={'f','n','k'};
>> c={'gh',4,[2 3]};
>> d=[a,b,c]
d = 1×9 cell 数组
  
>>  f=[a;b;c]
f =
  3×3 cell 数组
    {[ 1]}    {[2]}    {[       3]}
    {'f' }    {'n'}    {'k'       }
    {'gh'}    {[4]}    {1×2 double}
    
2.1.3 使用元胞数组构造运算符构造一个嵌套元胞数组
        
>> g={a;b;c}
g =
 
  3×1 cell 数组
    {1×3 cell}
    {1×3 cell}
    {1×3 cell}

3 combinedStr = strcat(s1, s2, ..., sN)
描述:将数组 s1,s2,...,sN 水平地连接成单个字符串

4、c = 'char';%字符
   s = "string";%字符串
4.1 字符变量可以被索引,而字符串变量不可以被索引。因此字符变量可以被当做一个由字符组成的一维向量。
4.2 disp、fprintf、fullfile等函数,两者可以互换。
4.3 字符串则是一个变量,如果需要对字符串进行造作,可以使用字符串相关函数,例如strcmp等。
4.4 利用char()和string()函数可以对字符串和字符变量相互转换。


5、转换
str2double 串转换为双精度值
str2mat 创建多行串数组
str2num 串转换为数
strcat 接成长串
strcmp 串比较
strjust 串对齐
    strjust()函数
    strjust ( str,‘right’ ):字符串右对齐
    strjust ( str,‘left’ ):字符串左对齐
    strjust ( str,‘center’ ):字符串居中对齐
strmatch 匹配指定串
    %粗匹配
    eg:x = strmatch('max',char('max','minmax','maxmum'))
    x = 1
        3
    %精准匹配
    eg:x = strmatch('max',char('max','minmax','maxmum'),'exact')
    x = 1
filesep  文件分隔符
>>iofun_dir = ['a' filesep 'b' filesep 'c']
>>a\b\c
strncmp 串中前若干字符比较
strrep 串替换
strtok 寻找第一间隔符前的内容
struct 创建构架数组
struct2cell 把构架转换为元胞数组
strvcat 创建多行串数组
sub2ind 多下标转换为单下标

str_TF = contains(str1,str2)  %查询str2 在 str1的位置,并且输出0/1

MATLAB字符串匹配函数中,应用较多的有:findstr、strfind、strmatch、strcmp、strmcmp。

findstr:在较长的字符串中查找较短的字符串出现的次数,并返回其位置
ss = ['agjaigjarugaifdvma','gagaergaergaerg']
ss =
    'agjaigjarugaifdvmagagaergaergaerg'
>> findstr(ss,'a')
ans =
     1     4     8    12    18    20    22    26    30
     
strfind:strfind(s1,pattern),因此其意思在s1中搜索pattern 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值