2018.9.15,Matlab實驗三:字串、單元陣列和結構體
一、實驗任務和目的 1. 掌握Matlab的字串常用函式及其操作方法。 2. 掌握Matlab的結構體的基本操作方法。 3. 掌握Matlab的元胞陣列的基本操作方法。 二、實驗內容 1. 字串陣列Str=[‘hopes, dreams, hold up, old up’],查詢’O’出現的次數和位置。 2. 現有三個字串變數s1=“i”,s2=“love”,s3=“matlab7.1”,利用字串處理函式,將其用空格連線在一起,並字母轉換為大寫,並將7.1替換為2016a。 3. Str=’ 1 The existing research is about location tracking either completely indoor or altogether on open air 2 by utilizing various sensors and procedures based on inter-networking or internet of things.’,對該字串做如下處理: (1)判斷字串中每個單詞的首字母是否大寫,若不是則將其修改為大寫,其他字母為小寫。 (2)統計字串中的數字和字母的個數。 (3)將字串中間的空格和數字刪除,所有字母倒過來重新排序。 4. 建立一個結構體,用於統計學生的情況,包括學生的姓名、學號、各科成績等。然後使用該結構體對一個班級的學生的成績進行管理,如計算總分、平均分、排列名次等。 5. 建立一個2X2的元胞陣列,第1、2個元素為字串,第3元素為整型,第4元素為雙精度型別,並將其用圖形表示。
三、實驗過程和結果 1. 字串陣列Str=[‘hopes, dreams, hold up, old up’],查詢’O’出現的次數和位置。
>> Str=['hopes, dreams, hold up, old up'];
p=findstr(Str,'O')
p =
[]
>> length(p)
ans =
0
2 . 現有三個字串變數s1=“i”,s2=“love”,s3=“matlab7.1”,,並字母轉換為大寫,並將7.1替換為2016a。
>> s1='i';
>> s2='love';
>> s3='matlab7.1' ;
>> s4=strcat(s1,32,s2,32,s3);
>> k=find(s4>='a'&s4<='z');
>> s4(k)=s4(k)-'a'+'A';
>> s4
s4 =
I LOVE MATLAB7.1
>> strrep(s4,'7.1','2016a')
ans =
I LOVE MATLAB2016a
3 . Str=’ 1 The existing research is about location tracking either completely indoor or altogether on open air 2 by utilizing various sensors and procedures based on inter-networking or internet of things.’,對該字串做如下處理: (1)判斷字串中每個單詞的首字母是否大寫,若不是則將其修改為大寫,其他字母為小寫。
>> Str=' 1 The existing research is about location tracking either completely indoor or altogether on open air 2 by utilizing various sensors and procedures based on inter-networking or internet of things.';
k=findstr(Str,' ');
l=length(k);
for j=1:l
if(Str(k(j)+1)>='a'&Str(k(j)+1)<='z')
Str(k(j)+1)=Str(k(j)+1)-'a'+'A';
end
end
>> Str
Str =
1 The Existing Research Is About Location Tracking Either Completely Indoor Or Altogether On Open Air 2 By Utilizing Various Sensors And Procedures Based On Inter-networking Or Internet Of Things.
(2)統計字串中的數字和字母的個數。
>> Str=' 1 The existing research is about location tracking either completely indoor or altogether on open air 2 by utilizing various sensors and procedures based on inter-networking or internet of things.';
>> k=find(Str>='0'&Str<='9');
>> m=find(Str>='a'&Str<='z');
>> length(k)
ans =
2
>> length(m)
ans =
162
(3)將字串中間的空格和數字刪除,所有字母倒過來重新排序。
>> Str=' 1 The existing research is about location tracking either completely indoor or altogether on open air 2 by utilizing various sensors and procedures based on inter-networking or internet of things.';
S=strrep(Str,' ','')
k=find(S>='0'&S<='9');
S(k)='';
revch=S(end:-1:1)
S =
1Theexistingresearchisaboutlocationtrackingeithercompletelyindoororaltogetheronopenair2byutilizingvarioussensorsandproceduresbasedoninter-networkingorinternetofthings.
revch =
.sgnihtfotenretnirognikrowten-retninodesabserudecorpdnasrosnessuoiravgnizilituybrianeponorehtegotlaroroodniyletelpmocrehtiegnikcartnoitacoltuobasihcraesergnitsixeehT
4 建立一個結構體,用於統計學生的情況,包括學生的姓名、學號、各科成績等。然後使用該結構體對一個班級的學生的成績進行管理,如計算總分、平均分、排列名次等。
這裡假設一個班有三名同學(不好意思,這個我暫時不會)
5 建立一個2X2的元胞陣列,第1、2個元素為字串,第3元素為整型,第4元素為雙精度型別,並將其用圖形表示。
>> A=cell(2,2);
>> A(1,1)={'i love'};
>> A(2,1)={'you'};
>> A(1,2)={int16(128)};
>> A(2,2)={double(16)};
>> cellplot(A)
四、實驗總結和心得 掌握了字串的查詢,連線,刪除,倒置,替換等一系列操作 掌握了結構陣列和元胞陣列的用法