0
votes

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

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

The output should be (Which i want):

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

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.

1
You may have better luck if you asked this on StackOverflow. The Stats SE is stronger in statistical methodology than programming questions. - Christopher Aden
Which three parameters do you want to extract? When you merged the files, did you simply append the individual files? - Jonas
What is the format of output file? - Jessada Thutkawkorapin
You can certainly do it in MATLAB, but Perl probably would be easier. You can run perl script directly from MATLAB with PERL function. Have you considered this option? - yuk

1 Answers

0
votes

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);

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.