天天看點

Matlab将資料存為文本檔案 - lakeone

Matlab将資料存為文本檔案

dlmwrite :将一個矩陣寫到由分隔符分割的檔案中。      
在儲存整數到檔案時使用save存為ascii檔案時,常常是檔案裡都是實型格式的資料(有小數點,和後面很多的0,看着很不友善)。于是要儲存此類資料時,我們可以使用此dlmwrite指令。
使用方法:
dlmwrite(\'filename\', M)
使用預設分隔符“,”将矩陣M寫入文本檔案filename中;
dlmwrite(\'filename\', M, \'D\') 
使用分隔符D分割資料,“\t”表示tab分割,“,”為預設分割符;
dlmwrite(\'filename\', M, \'D\', R, C)
從矩陣M的第R行、第C列開始,作為要寫矩陣塊的左上角,将資料用D分割寫入檔案。
其他用法有:
dlmwrite(\'filename\', M, \'attrib1\', value1, \'attrib2\', value2, ...)
dlmwrite(\'filename\', M, \'-append\')
dlmwrite(\'filename\', M, \'-append\', attribute-value list) 
例如: 
a = [1 2 3; 4 5 6; 7 8 9];
dlmwrite(\'test.txt\', a);
則test.txt中的内容為:
1,2,3
4,5,6
7,8,9
而使用save 
a = [1 2 3; 4 5 6; 7 8 9];
save \'tst.txt\' a -ascii;
文本檔案裡的内容為:
          1.0000000e+000          2.0000000e+000          3.0000000e+000
          4.0000000e+000          5.0000000e+000          6.0000000e+000
          7.0000000e+000          8.0000000e+000          9.0000000e+000