简体   繁体   中英

Reading Data from more than 2000 txt files in matlab

I have more than 3000 Data txt files. I create the name for these file according to special rate h=[1]; k+=[0.1 0.2 0.4 0.7 1 1.1 1.2 1.5 1.7 2 2.5 3 3.5 4 5]; KT=[1e-6 0.1 0.2 0.4 0.7 1 1.1 1.2 1.5 1.7 2 2.5 3 3.5 4 5]; KD=[1e-6 0.1 0.2 0.4 0.7 1 1.1 1.2 1.5 1.7 2 2.5 3 3.5 4 5]; h=[1]; k+=[0.1 0.2 0.4 0.7 1 1.1 1.2 1.5 1.7 2 2.5 3 3.5 4 5]; KT=[1e-6 0.1 0.2 0.4 0.7 1 1.1 1.2 1.5 1.7 2 2.5 3 3.5 4 5]; KD=[1e-6 0.1 0.2 0.4 0.7 1 1.1 1.2 1.5 1.7 2 2.5 3 3.5 4 5];

The Name of each files like this one (MTN100_k+0.1_k-T_0.1_k-D0.4_h1_GTP0.txt) How can I read these files in Matlab. I need to work with each file separately. I tried to use dlmread and fscan but it is not helpful. I need some programming, just write an algorithm to generate the correct file name automatically.

for example if my file names is:

MTN100_k+0.1_k-T_0.1_k-D0.4_h1_GTP0.txt)

MTN100_k+0.1_k-T_0.1_k-D0.7_h1_GTP0.txt)

MTN100_k+0.1_k-T_0.1_k-D1_h1_GTP0.txt)

h1 and GTP0 are fixed I just need to change k+,kT,kD. I am mainly using C++ and not really familiar with how to do the similar things in MATLAB. Any help would be greatly appreciated. I will be grateful to you.

Salam 3lykum!

Generally, you should do what rayryeng suggested in his comment, which may be done like this:

cd(fullPathOfTheFolderWithTheTxtFiles);
filenames0 = dir('*.txt');
filenames0 = {filenames0.name}'; ...'//See documentation of dir.

See: cd , dir .

However, if you are interested in how a list of filenames can programmatically be generated, I'm providing the following answer.

I am assuming that all possible combinations for filenames exist for k+ , kT and KD .

For the sake of explanation, let's define 3 vectors as follows:

k_plus =[0.1 0.2 0.4 0.7 1 1.1 1.2 1.5 1.7 2 2.5 3 3.5 4 5];
K_minus_T = [1e-6 0.1 0.2 0.4 0.7 1 1.1 1.2 1.5 1.7 2 2.5 3 3.5 4 5];
K_minus_D = [1e-6 0.1 0.2 0.4 0.7 1 1.1 1.2 1.5 1.7 2 2.5 3 3.5 4 5];

What I'll do first is to generate a list of all possible combinations of these vectors :

sets = {k_plus, K_minus_T, K_minus_D};
[x,y,z] = ndgrid(sets{:});
cartProd = [x(:) y(:) z(:)];

Next you can either write a loop, or do this with a slightly less easy to read vectorized version:

 %// Loop Version:
nFiles = size(cartProd,1);
filenames1{nFiles,1}=[]; %// Preallocation
for ind1=1:nFiles
    filenames1{ind1} = ['MTN100_' ...
        'k+'  num2str(cartProd(ind1,1)) '_' ...
        'k-T' num2str(cartProd(ind1,2)) '_' ...
        'k-D' num2str(cartProd(ind1,3)) '_' ...
        'GTP0.txt'];
end
%// Vectorized Version:
filenames2=cellstr([repmat('MTN100_k+',[nFiles,1]) num2str(cartProd(:,1)) ...
                    repmat('_k-T',[nFiles,1]) num2str(cartProd(:,2)) ...
                    repmat('_k-D',[nFiles,1]) num2str(cartProd(:,3)) ...
                    repmat('_GTP0.txt',[nFiles,1])]);    

Then you could loop over the resulting array and do whatever you need with the files.

Alternatively, if you want to find the filename of a specific combination of values, you could use something like this:

kP_wanted = k_plus(4);
kT_wanted = K_minus_T(9);
kD_wanted = K_minus_D(2);

filenames{intersect(intersect(find(cartProd(:,1)==kP_wanted),...
                              find(cartProd(:,2)==kT_wanted)),...
                              find(cartProd(:,3)==kD_wanted))}
 clc;
    clear all;
    close all;
    %%
    h=[1];
    k_plus =[0.1 0.2 0.4 0.7 1 1.1 1.2 1.5 1.7 2 2.5 3 3.5 4 5];
    K_minus_T =[1e-6 0.1 0.2 0.4 0.7 1 1.1 1.2 1.5 1.7 2 2.5 3 3.5 4 5];
    K_minus_D = [1e-6 0.1 0.2 0.4 0.7 1 1.1 1.2 1.5 1.7 2 2.5 3 3.5 4 5];
    sets = {k_plus, K_minus_T, K_minus_D,h};
    [x,y,z r] = ndgrid(sets{:});
    cartProd = [x(:) y(:) z(:) r(:)];
    nFiles = size(cartProd,1);
    filename{nFiles,1}=[];
    for i=1:nFiles
        filename{i} = ['MTN100_' ...        
            'k+'  num2str(cartProd(i,1)) '_' ...
            'k-T_' num2str(cartProd(i,2)) '_' ...
            'k-D' num2str(cartProd(i,3)) '_' ...
            'h'  num2str(cartProd(i,4)) '_' ...
            'GTP0.txt'];
            end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM