使用MATLAB将多个文件合并为一个文件 [英] Merging multiple files into one file by using MATLAB

查看:2471
本文介绍了使用MATLAB将多个文件合并为一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我正在共享.dat文件中的数据之一.我有16162个不同的文件.我将所有内容合并到一个文件中,并希望在matlab中读取它,并且需要从单个文件中提取三个参数的值,然后将它们按行或列进行排列.我可以通过使用C尖锐的代码来做到这一点,但我想通过使用matlab来做到这一点.有人可以帮我写代码吗?

Here i am sharing one of my data which are in .dat file. I have 16162 different files. I merged all into one file and want to read it in matlab and need to extract three parameter's values from a single file and arrange them in either row wise or column wise. I can do it by using C sharp codes but i want to do it by using matlab. can anybody help me out for writing the codes please?

这是一个示例文件数据:

Here is the one sample file data:

DISTRIBUTION: monomodal log-normal
n    :  1.000
r_mod:   .010
sigma:  1.400

number conc., surface. conc., volume conc. 

 .1087E+01     .1866E-02       .7878E-05

part. ave. radius, surf. ave. radius, vol. ave. radius : 
 .1149E-01   .1169E-01   .1201E-01

surface mean radius, volume mean radius : 
 .1267E-01   .1392E-01

eff. variance : 
 .9939E-01

比方说:我想提取或读取三个参数(r_mod,sigma,Surface表示半径).我在此页面中放入的文件中这三个参数的对应值为.010,1.400,.1267E-01

Let's say: I want to extract or read three parameters (r_mod, sigma, Surface means radius). The corresponding values for these three parameters from the file I put in this page is .010 , 1.400 , .1267E-01

输出应为(我想要):

r_mod sigma surface mean radius .01 1.4 1.27E-02 .02 1.4 2.67E-02 .03 1.4 3.98E-02 ... .. .. .. .. .. .. .. ..

我在同一目录中有成千上万个类似文件.我想在matlab中读取所有这些文件,并且输出应该以这种方式显示在单个文件中.

I have more than thousands similar files in a same directory. I want to read all those files in matlab and the output should show in this way in a single file.

推荐答案

鉴于所有文件都具有完全相同的结构,下面将完成此工作(只需确保对代码中的注释进行扩孔,您将需要进行调整您的文件名和要读取的文件数):

Given that all files have the exact same structure, the following will do the job (just make sure to ream the comments in the code, you will need to adapt your file names and number of files to read):

n = 2; % Number of files you want to go through
vals = zeros(1,3*n);
str = 'r_mod sigma surface mean radius        ';
k = 1;
for i = 1:n
    path = ['myFile',num2str(i),'.dat']; % change this to fit your file names
    fid = fopen(path, 'rb');
    data = textscan(fid,'%s');
    fclose(fid);
    data = data{1};
    vals(k) = str2double(data{8});
    vals(k+1) = str2double(data{10});
    vals(k+2) = str2double(data{40});
    k = k+3;
end
out = [str, num2str(vals)];
fid = fopen('output.txt', 'w');
fprintf(fid,out);

文件output.txt现在包含所需的行.如果您还希望输出文件也为.dat,则可以更改格式.

The file output.txt now contains your desired line. You may change the format if you want the output file to be .dat as well.

这篇关于使用MATLAB将多个文件合并为一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆