MATLAB Batch processing mat

Source: Internet
Author: User
Tags clear screen

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.