A_En_MATLAB-File-Operation-Summary

本文详细介绍了在MATLAB中进行文件读写操作的方法,包括直接读写MATLAB类型的文件(.mat)、使用csvwrite/csvread等函数处理CSV/TXT文件,以及通过fopen和fprintf进行文本文件的操作。同时,也提供了保存图像到文件的技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

layouttitledatetagscategories
MATLAB
MATLAB File Operation Summary
2019-04-07 06:17:57 -0700
MATLAB
技术
EnglishNotes

In doing research,We often need output the result to the file and read from the file.In this note, I will summarize the methods of reading and output method in MATLAB.

Deirectly read and write

mat file

We first consider the method of saving and read the MATLAB type file. The Matrix in MATLAB can be directly saved in a "xx.mat" file. This method can save many different result in an whole single file. I really recommend this method. This can be done as follows:

filename=['sweep_result_',datestr(now,'yyyy_mm_dd_HHMMSS'),'.mat'];
save(num2str(filename),'matrixa','matrixb','matrixc');

to read the file,

load('filename')

csv,txt file

the mat file can only be opened in MATLAB, In order to allow other software to open the output file. We can save the file as txt or csv file. This can be done via csvread/cavwrite, textread/textwrite,dlmwrite/dlmread,And I recommend the dlm method

%Write matrix M to a file, 'myFile.txt', delimited by the tab character and using a precision of 3 significant digits
dlmwrite('myFile.txt',M,'delimiter','\t','precision',3)
%Export a matrix to a file named myfile.txt. Then, append an additional matrix to the file that is offset one row below the first
X = magic(3);
dlmwrite('myfile.txt',[X*5 X/5],' ')
dlmwrite('myfile.txt',X,'-append', ...
   'roffset',1,'delimiter',' ')
%ead the entire file using dlmread.
M = dlmread('myfile.txt')

fopen method

this can be done via fopen and fprintf method

fid=fopen('progress.txt','at');
fprintf(fid,[datestr(now),' the loop step is %d \n'],lk4);
fclose(fid);

"print" output image to file

to save the figure in the window,the best way is to use print.

filename=['xxxxx_','.png'];
print(num2str(filename), '-dpng', '-r600')%for eps,'-deps2'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值