These days, people are constantly asking how to convert txt into mat format in bulk. Mat format storage occupies little space.
Here, for example, the site data of the China Meteorological Bureau, the conversion procedure is as follows:
CLC% Clear Screen
Clear% clear Memory variable
filenames = ls (' *.txt ');% lists all txt files under that path as an array of strings
[m n] = size (filenames);% gets the number of rows filenames
for j = 1:m
Fid=dlmread (char (Filenames (J,:)));
Matname=char (Filenames (j,1:6));
Save (Matname, ' fid '),% converted to mat format
% Xlswrite (matname,fid),% to XLS format
Jdt=[matname, ' conversion complete! ';
Disp (JDT)
End
After the above program has been converted, multiple mat formats need to be merged into one file:
CLC% Clear Screen
Clear% clear Memory variable
filenames = ls (' *.mat ');% lists all txt files under that path as an array of strings
[m n] = size (filenames);% gets the number of rows filenames
G=[];
for j = 1:m
D=load (Filenames (j,:));
E=struct2cell (d);
F=cell2mat (e);
Clear d e;
G=cat (1,G,F);
End
Save (' dailyprecipitation ', ' G ');
There are also people who need to store data according to the site, as mentioned in the previous article. The procedure is as follows:
CLC% Clear Screen
Clear% clear Memory variable
filenames = ls (' *.mat ');% lists all txt files under that path as an array of strings
[m n] = size (filenames);% gets the number of rows filenames
for j = 1:m
G=load (Filenames (j,:));
E=struct2cell (g);
Clear G;
F=cell2mat (e);
Clear e;
Aa=f (:, ten);
AA (aa>20000) =0;
F (:, ten) =AA./10;
F=sortrows (f,[1,5,6,7]);
[u v] = unique (f (:, 1));% find different numbers in the first column and where they are located
q = 1;
For i = 1:length (v)-1
matname=num2str (U (i));
R=f (Q:v (i+1)-1,:);
Save ([Matname, '. txt '], ' r ', '-ascii ');% write
q = V (i+1);
End
End