How to write data to a file in matlab

      When MATLAB data is collected, it is often necessary to save the obtained data.

fid = fopen(filename, 'open with');

Description: fid is used to store the file handle value. If fid>0, it means that the file is opened successfully. The options for opening are as follows: 
'r': To open the file in read-only mode (the default), the file must already exist. 
'r+': Open the file in read and write mode, after opening, read first and then write. The file must already exist. 
'w': write data after opening. Update if the file already exists; create if it does not exist. 
'w+': Open the file for reading and writing. Read first and then write. Update if the file already exists; create if it does not exist. 
'a': Append data at the end of the open file. Create the file if it does not exist. 
'a+': After opening the file, read the data first and then add the data. Create the file if it does not exist. 
In addition, adding a "t" after these strings, such as 'rt' or 'wt+', will open the file in text mode; if you add a "b", it will open in binary format, which is also the default of the fopen function. way of opening.

1. If it is just a simple variable, it can be written as follows. g2ggg is the file name, txt file (of course it can be changed to other file formats), this is automatically saved by matlab under the working file, and baocun is the variable we want to save. %d represents an integer, and \r\n represents a newline every time the data is written.

fid = fopen('g2ggg.xls','a');
fprintf(fid,'%d \t ',baocun);
fprintf(fid,'\r\n'); % newline
fclose(fid);
in:

fid = fopen('g2ggg.xls','a');

The 'a' in this sentence stands for subsequent writes. In this way, the previously saved data will not be replaced each time the program is re-run, but it will still be saved in the previously saved folder, and then the previous data will continue to be saved.

But if this place is 'w', it means writing, it will overwrite the previous data once it writes data, and finally you will find that only one data is saved in the txt file. Unless you define a matrix in the program, store the data in the matrix every time, and save the matrix at the last time, all the data will be saved, but remember that the txt folder before you save this time will be saved this time. This folder is replaced, unless you name the folder differently this time.

Second, if it is to save the matrix, it can be written as follows. save_data is the file name, txt file, this is automatically saved by matlab under the working file, and baocun is the matrix we want to save. %d represents an integer, and \r\n represents a newline every time the data is written.

fid = fopen('save_data.txt','a');
[r,c]=size(baocun);
for i=1:r
for j=1:c
fprintf(fid,'%5f\t',baocun(i,j));
end
fprintf(fid,'\r\n');
end
fclose(fid);   

in:

fid = fopen('save_data.txt','a');
The 'a' in this sentence stands for subsequent writes. In this way, the previously saved data will not be replaced each time the program is re-run, but it will still be saved in the previously saved folder, and then the previous data will continue to be saved.

But if this place is 'w', it means writing, it will overwrite the previous data every time it writes data, and finally you will find that only one data is saved in the txt file. Unless you define a matrix in the program, store these matrices in the defined matrix each time, and save the matrix at the last time, all these data will be saved, but remember that the txt folder before you save this time will Replaced by this folder, unless you named the folder differently from last time.


3. The first two cases are all under the working path automatically saved by matlab. What if we need matlab to be automatically saved in the specified path? It can be written like this:

fid = fopen('C:\Users\Desktop\g2ggg.txt','a');  
fprintf (fid, '% d,% d,% d \ r \ n', baocun);
in

C:\Users\Desktop\
代表保存路径,这里保存在电脑桌面。

四、如果需要手动输入保存路径,可以像这样写:
[FileName,PathName]=uiputfile({'*.txt','Txt Files(*.txt)';'*.xls','Excel(*.xls)';'*.*','All Files(*.*)'},'choose a File');  %% pathname获取保存数据路径, filename获取保存数据名称
if ~FileName
return;
else
str= [PathName,FileName];
fid = fopen(char(str), 'w');   % 要想存的文件名是自己输入的,这个地方就得这样写
fwrite(fid, '', 'integer*4');
[r,c]=size(baocun);            % 得到矩阵的行数和列数
 for i=1:r
  for j=1:c
  fprintf(fid,'%d\t',baocun(i,j));
  end
  fprintf(fid,'\r\n');
 end
fclose(fid);
一般这样都是采集完成后由手工保存的,在这里保存的是矩阵,因为矩阵里面记录了之前每次的数据,所以采集完成后,一次性保存矩阵就可以保存所有数据了,一般这段代码在GUI里面添加按钮的回调函数里面。每次按下按钮就会弹出窗口让输入文件名以及选择保存路径。
这里增加了这段代码,又修改了一小部分代码,认真看就知道了。
[FileName,PathName]=uiputfile({'*.txt','Txt Files(*.txt)';'*.xls','Excel(*.xls)';'*.*','All Files(*.*)'},'choose a File');  %% pathname获取保存数据路径, filename获取保存数据名称
if ~FileName
return;
else
str= [PathName,FileName];
fid = fopen(char(str), 'w');   

五、如果有需要保存字符串数组的话:
baocun={'R','G','B','Xdata'};  
这个是包含字符串的数组baocun,
fid = fopen('save_data.txt','a');
 fwrite(fid, '', 'integer*4');
 for n=1:4
 fprintf(fid,'%s\t',char(baocun{n}));    %   \t表示空格
 end
 fprintf(fid,'\r\n');   %换行
 fclose(fid);

很多东西都是举一反三,多动脑,多尝试,实在没办法就上网找答案,一般都能找到。





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324881772&siteId=291194637