ASCII code n n n double ASCII char
字串和其ASCII code n n n double 指令: 檢視字串變數的儲存內容(即 ASCII 內碼) char 指令: 將 ASCII 內碼轉回字串形式 範例10 -6: string 106. m sentence = 'I''ve got a date!'; sentence. Ascii = double(sentence); %檢視 sentence 的 ASCII 碼 sentence 2 = char(sentence. Ascii) % 將 ASCII 碼轉回字串形式 phraseascii = Columns 1 through 12 73 39 118 101 32 103 111 116 32 97 32 100 Columns 13 through 16 97 116 101 33 sentence 2 = I've got a date! 2009/10/27 6
ASCII碼對照表 (2/3) ASCII碼 鍵盤 ASCII 碼 鍵盤 27 ESC 32 SPACE 33 ! 34 " 35 # 36 $ 37 % 38 & 39 ' 40 ( 41 ) 42 * 43 + 44 ' 45 - 46 . 47 / 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 56 8 57 9 58 : 59 ; 60 < 61 = 62 > 63 ? 64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G 72 H 73 I 74 J 75 K 76 L 77 M 78 N 2009/10/27 鍵盤 8
ASCII碼對照表 (3/3) ASCII碼 鍵盤 ASCII 碼 鍵盤 79 O 80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W 88 X 89 Y 90 Z 91 [ 92 93 ] 94 ^ 95 _ 96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g 104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o 112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w 120 x 121 y 122 z 123 { 124 | 125 } 126 ~ 2009/10/27 9
字元與字串的基本概念-eval n n eval 指令: 直接“執行”某一特定字串,其效果就如同直接在 MATLAB 指 令視窗內輸入此一特定字串 eval 指令特別適用於在 for - loop 內自動產生有規律的變數名稱 範例10 -11: string 111. m clear all for i = 3: 6 % 清除所有變數 eval(['x', int 2 str(i) , '= magic(' , int 2 str(i) , ') ; ']); end whos x* Name x 3 x 4 x 5 x 6 Size 3 x 3 4 x 4 5 x 5 6 x 6 Bytes Class 72 128 200 288 double array Grand total is 86 elements using 688 bytes n x 3 , x 4 , x 5 , x 6 都是在 for - loop 中產生的變數,分別代表維度為 3× 3、 4× 4、5× 5、6× 6 的魔方陣 2009/10/27 11
10 -3 字串的比較、尋找、 代換、分解與結合 n strcmp 指令: 用於比較字串內容的異同 範例10 -19: string 301. m str 1 = 'today'; str 2 = 'tomorrow'; str 3 = 'today'; out 1 = strcmp(str 1, str 2) out 2 = strcmp(str 1, str 3) n n % 比較兩字串 str 1 和 str 2 = 0 % 比較兩字串 str 1 和 str 3 = 1 不相等回傳 0, 相等回傳 1 DIFFERENT to C 2009/10/27 16
字串的分解 n n n strtok 指令: 根據一給定的分界字元(Delimiting Characters),將一字 串拆解成數個字串,預設分界字元為空白字元 strvcat 指令: 將拆解下來的字串內容(即儲存在字串變數 chopped 中) 加到二維字元陣列 parsed 中 範例10 -24: string 306. m input_string = 'ee cs econ stat me'; remainder = input_string; parsed = ''; % 建立一空字元陣列 while (any(remainder)) [chopped, remainder] = strtok(remainder); parsed = strvcat(parsed, chopped); end parsed = ee cs econ stat me 2009/10/27 18
- Slides: 22