相关文章推荐

在 MATLAB 中,數個字元(Characters)可以構成一個字串(Strings),一個字串是被視為一個列向量(Row Vector)進行儲存,而此一字串中的每一字元(含空白字元),是以其 ASCII 碼的形式存放於此列向量中的每一個元素(Element),只是其外顯形式仍然是可讀的字元。例如:

Example 1: 10-字元與字串/string101.m sentence = 'Draft beer, not people' % 建立字串變數 sentence 
 sentence =
 Draft beer, not people
 <p>多個字串變數可直接並排,以得到一個新字串變數,例如:
 </p><p><a name="10-字元與字串/string102"/></p><fieldset><legend>Example 2: <a href="example/10-字元與字串/string102.m">10-字元與字串/string102.m</a></legend><xmp class="code">str1 = 'I like MATLAB,'; % 建立字串變數 str1
 str2 = ' JavaScript, and C++!'; % 建立字串變數 str2
 str3 = [str1 str2] % 直接橫列並排字串變數 str1 及 str2,
 % 以建立一個新字串變數 str3 
 str3 =
 I like MATLAB, JavaScript, and C++!

Hint
MATLAB 用「單引號(Single Quote)」來界定字串變數,而 C 語言則是用「雙引號(Double Quote)」來界定字串變數,這是兩者不同之處。(一點小牢騷:為什麼 MATLAB 不和 C 語言一樣呢?害死了我們這些辛苦的程式設計師了!)

欲輸入含有單引號的字串,可重覆單引號的使用,例如:

Example 3: 10-字元與字串/string103.m sentence = 'I''ve got a date!' % 重覆「'」的使用 
 sentence =
 I've got a date!
 <p>若要計算字串變數的長度(即組成字元的個數),可用 length 指令:
 </p><p><a name="10-字元與字串/string104"/></p><fieldset><legend>Example 4: <a href="example/10-字元與字串/string104.m">10-字元與字串/string104.m</a></legend><xmp class="code">sentence = 'I''ve got a date!';
 length(sentence) % 計算字串變數 sentence 的長度 
 ans =
 <p>若要檢視字串變數的儲存內容(即 ASCII 內碼),可使用 double 指令,例如:
 </p><p><a name="10-字元與字串/string105"/></p><fieldset><legend>Example 5: <a href="example/10-字元與字串/string105.m">10-字元與字串/string105.m</a></legend><xmp class="code">sentence = 'I''ve got a date!';
 sentenceAscii = double(sentence) % 檢視 sentence 的 ASCII 碼 
 sentenceAscii =
 Columns 1 through 11
 73 39 118 101 32 103 111 116 32 97 32
 Columns 12 through 16
 100 97 116 101 33
 <p>若要將 ASCII 內碼轉回字串形式,可用 char 指令,例如:
 </p><p><a name="10-字元與字串/string106"/></p><fieldset><legend>Example 6: <a href="example/10-字元與字串/string106.m">10-字元與字串/string106.m</a></legend><xmp class="code">sentence = 'I''ve got a date!';
 sentenceAscii = double(sentence);
 sentence2 = char(sentenceAscii) % 將 ASCII 碼轉回字串形式 
 sentence2 =
 I've got a date!
 <p>由於 MATLAB 在儲存字串時,無論是中文或英文,每一個字元都會佔用兩個位元組(2 Bytes),故在上例中,字串變數 sentence 總共由 16 個字元構成,佔用的記憶體總計為三十二個位元組(32 bytes),可使用 whos 指令來檢視字串變數 sentence 所佔用儲存空間:
 </p><p><a name="10-字元與字串/string107"/></p><fieldset><legend>Example 7: <a href="example/10-字元與字串/string107.m">10-字元與字串/string107.m</a></legend><xmp class="code">sentence = 'I''ve got a date!';
 whos sentence % 檢視工作空間內變數 sentence 佔用記憶體大小 Name Size Bytes Class Attributes
 sentence 1x16 32 char 
 <p>由於 MATLAB 是以兩個位元組來儲存一個字元,所以也可以支援 Big5 的中文碼,而且 Big5 中文的 ASCII 內碼都會大於數字 128,可驗證如下:
 </p><p><a name="10-字元與字串/string108"/></p><fieldset><legend>Example 8: <a href="example/10-字元與字串/string108.m">10-字元與字串/string108.m</a></legend><xmp class="code">chinese = '今日事,今日畢';
 abs(chinese) % 檢驗中文內碼 
 ans =
 Columns 1 through 5
 20170 26085 20107 65292 20170
 Columns 6 through 7
 26085 30050
 <p>請注意,由於 MATLAB 將字串以其相對應之 ASCII 內碼(即數字形式)儲存成一列向量,故若對此字串直接進行數值運算,MATLAB 會先將此字串轉成數值,再進行一般數值向量的運算,例如:
 </p><p><a name="10-字元與字串/string109"/></p><fieldset><legend>Example 9: <a href="example/10-字元與字串/string109.m">10-字元與字串/string109.m</a></legend><xmp class="code">chinese = '今日事,今日畢'; 
 x = chinese+1 
 Columns 1 through 5
 20171 26086 20108 65293 20171
 Columns 6 through 7
 26086 30051

Hint
對於中文的處理,MATLAB 在第六版(含)以前是使用大五碼,但在第七版開始,改用 unicode,因此上述兩個範例,若你使用第六版來執行,得到的結果是以大五碼的內碼數值來顯示,和我們的範例結果會不一樣。

若要直接"執行"某一特定字串,可以使用 eval 指令,其效果就如同直接在 MATLAB 指令視窗內輸入此一特定字串,例如:

Example 10: 10-字元與字串/string110.m str = 'x = [1 2 3]; y = x.^2'; 
 eval(str) 
 1 4 9
 <p>此結果和由 MATLAB 命令視窗內直接輸入「x = [1 2 3]; y = x.^2」的結果是完全相同的。eval 指令特別適用於在 for - loop 內自動產生有規律的變數名稱,例如:
 </p><p><a name="10-字元與字串/string111"/></p><fieldset><legend>Example 11: <a href="example/10-字元與字串/string111.m">10-字元與字串/string111.m</a></legend><xmp class="code">clear all % 清除所有變數
 for i = 3:6 
 eval(['x', int2str(i) , '= magic(' , int2str(i) , ') ; ']); 
 whos x* Name Size Bytes Class Attributes
 x3 3x3 72 double 
 x4 4x4 128 double 
 x5 5x5 200 double 
 x6 6x6 288 double 
 <p>由上可知 x3 , x4 , x5 , x6 都是在 for - loop 中產生的變數,分別代表維度為 3×3、4×4、5×5、6×6 的魔方陣。(魔方陣的性質是:其直行、橫列及對角線的元素值總和均相等。)
 </p><p/><fieldset><legend>Hint</legend><div class="hint"><ul><li>另一個和 eval 很像的指令是 feval,它可以使用字串來代表函式名稱,並進行對此函式的呼叫,例如:feval('magic', 5) 和 magic(5) 是相同效果的,都會傳回一個維度為 5X5 的魔方陣。</li><li>mfilename 指令可傳回此指令所在之 M 檔案(請見本書第十五章)的檔案名稱。</li></ul></div></fieldset><p>
 
推荐文章