一、matlab寫入txt文件某一行
在matlab中,如果需要寫入txt文件的某一行,可以使用下面的代碼:
fid = fopen('example.txt','a+');
fprintf(fid,'%s\n', 'this is a new line');
fclose(fid);
其中,example.txt是需要寫入的txt文件名,a+表示以追加的方式打開txt文件。
二、matlab生成txt文件
如果需要在matlab中生成新的txt文件,可以使用下面的代碼:
fid=fopen('example.txt','w');
fprintf(fid,'%s','this is a new file');
fclose(fid);
其中,example.txt是需要生成的txt文件名,w表示以寫的方式打開txt文件。
三、matlab將數據寫入txt
matlab中可以使用dlmwrite函數將數據寫入txt文件。下面是一個例子:
x = [1 2 3; 4 5 6; 7 8 9];
dlmwrite('example.txt',x);
這樣就將矩陣x寫入到了文件中。
四、matlab數據保存成dat文件
與生成txt文件類似,可以使用下面的代碼將數據保存成dat文件:
x = [1 2 3; 4 5 6; 7 8 9];
save('example.dat','x','-ascii');
其中,-ascii表示使用ascii碼保存文件。
五、matlab保存文本文件
如果需要將matlab的運行結果保存成txt文件,可以使用下面的代碼:
diary('example.txt');
# 運行結果
diary off;
這樣就將運行結果保存到了example.txt文件中。
六、matlab寫入文件
在matlab中,可以使用fwrite函數將數據寫入文件。下面是一個例子:
fid = fopen('example.bin','w');
fwrite(fid,[1;2;3],'int');
fclose(fid);
這樣就將數字1、2、3寫入example.bin文件中。
七、matlab寫入bin文件
除了使用fwrite函數外,還可以使用matlab的save函數將數據保存成二進位格式的文件:
x = [1 2 3; 4 5 6; 7 8 9];
fid = fopen('example.bin','w');
fwrite(fid,x,'double');
fclose(fid);
這樣就將矩陣x以二進位的形式保存到example.bin文件中。
八、如何將matlab運行結果寫入txt文件
在matlab中,如果需要將運行結果寫入txt文件,可以使用下面的代碼:
diary('example.txt');
# 運行的命令行代碼
diary off;
這樣就將運行結果保存到了example.txt文件中。
九、matlab導入txt文件
如果需要將txt文件讀取到matlab中,可以使用load函數或者textread函數:
x = load('example.txt');
x = textread('example.txt');
其中,example.txt是要讀取的文件名。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/190316.html