سؤال

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. (cm^-3) (mu^2cm^-3) (mu^3cm^-3) .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.

هل كانت مفيدة؟

المحلول

If all your files are strictly identical except for the numerical values you could try to use the Matlab function textscan and specify the format of each line.

Here is an example of how you could do it:

fid=fopen('text.dat','r');
format={'DISTRIBUTION: monomodal log-normal n : %f r_mod: %f sigma: %f'; ...
        'number conc., surface. conc., volume conc. (cm^-3) (mu^2cm^-3) (mu^3cm^-3) %f %f %f'; ...
        'part. ave. radius, surf. ave. radius, vol. ave. radius : %f %f %f'; ...
        'surface mean radius, volume mean radius : %f %f'; ...
        'eff. variance : %f'};
data=cell(numel(format),1);
for i=1:numel(format)
    data{i}=textscan(fid,format{i},1);
end
fclose(fid);

In this example you can access the numerical values in the cell array data, r_mod is data{1}{2}, sigma is data{1}{3} and Surface means radius is data{4}{1} and so on. Then it is only a matter of organizing these data and saving the collection to file using fprintf to write the headers and dlmwrite to append the data.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top