matlab常用的字串操作函式之一

2021-07-30 12:56:09 字數 2726 閱讀 2509

strcat:依次橫向連線字串;

strvcat:依次縱向連線字串;

例項1:

>>a1='sophia ';

>>a2='is a ';

>>a3='good girl';

>>strres1=strcat(a1,a2,a3)

strres1 =

sophiais agood girl

例項2:

>>strres2=strvcat(a1,a2,a3)

strres2 =

sophia

is a

good girl

char:

建立二維陣列。

每行長度一致,短的用空格補齊;

將字串單元陣列轉化為二維字元陣列。

ischar:

判斷是否為字元陣列。

例項1:

>>charres1=char(a1,a2,a3)

charres1 =

sophia

is a

good girl

>>len=[length(charres1(1,:)),length(charres1(2,:)),length(charres1(3,:))]

len =

9 9 9

>>cellstr=;

>>charres2=char(cellstr)

charres2 =

sophia

is a

good girl

例項2:

>>ischarres1=ischar(a1)

ischarres1 =

1>>number=1;

>>ischarres1=ischar(number)

ischarres1 =

0

cellstr:

生成字串單元陣列。

iscellstr:

判斷是否為字串單元陣列。

例項1:

>>cellstrres=cellstr(a1)

cellstrres =

'sophia'

>>size(cellstrres)

ans =

1 1>>class(cellstrres)

ans =

cell

例項2:

>>iscellstr(cellstrres)

ans =

1>>iscellstr(a1)

ans =

0

blanks:

建立包含n個空格的字串。

deblank:

去掉字串或單元陣列所包含的字串尾部空格。

例項1:

>>blankres=blanks(4)

blankres =

>>class(blankres)

ans =

char

>>length(blankres)

ans =

4例項2:

>>deblankres='sophia is a good girl! ';

>>length(deblankres)

ans =

25>>deblankres=deblank(deblankres)

deblankres =

sophia is a good girl!

>>length(deblankres)

ans =

22

sprintf:

[s,errmsg]=sprintf(format,a,...)按格式format寫a的資料到字串s中。

sscanf:

a=sscanf(s,format,size)按格式從字串s中讀取資料。

例項1:

>>num=12;

>>sprintfres=sprintf('%05d',num)

sprintfres =

00012

例項2:

>> a='feifei only worth 0.01 rmb.';

>> our=sscanf(a,'%s %s %s %f %s')

our =

0.0100

>> our=sscanf(a,'%s',2)

our =

feifeionly

eval:

執行由mailab表示式組成的字串。

feval:

只執行函式。

evalin:

實現在工作空間中某個表示式字串,並將結果返回。

evalin(ws,'expression'),其中,ws為工作空間名稱,'expression'為表示式符號串。

例項1:

>>eval('1+2')

ans =

3例項2:

>> feval('sin',3)

ans =

0.1411

例項3:

>> a=12;

>> b=evalin('base','a+5')

b =17

注意:指令碼檔案所在工作空間為'base',函式所在的工作空間為函式名。

matlab 字串操作

a hello b word 1.獲取字串長度 length a ans 5 2.連線兩個字串,每個字串最右邊的空格被裁切 strcat a,b ans hello word 連線多行字串,每行長度可不等,自動把非最長字串最右邊補空格 使與最長字串相等,會忽略空字串 e strvcat a,b,m ...

matlab 字串操作

strrep 字串查詢與替換 sprintf 格式化輸出資料到字串 strcmp 字串比較,後者忽略字母大小寫 sscanf 格式化從字串中讀取資料 regexp,regexpi 匹配正規表示式,後者忽略大小寫 regexprep 使用正規表示式替換字串 regexptranslate 將私服穿轉化...

c語言string h中常用的字串操作函式

根據指定字元切分字串char strtok char str1,char str2 char input 26 i,am,a,boy char p p strtok input,while p 字串反轉char strrev char str char forward string printf b...