1

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]; K-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-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];

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+,k-T,k-D. 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.

Kh_Ameen
  • 97
  • 10
  • 1
    Use `dir` to get a list of files that are `.txt`, then use a `for` loop to iterate through each filename and load in the data. – rayryeng Aug 19 '14 at 18:09

2 Answers2

1

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+, k-T and K-D.

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))}
Community
  • 1
  • 1
Dev-iL
  • 23,742
  • 7
  • 57
  • 99
  • Thanks Dev-iL, your code is really helpful. I did some change on it and I got the files name as I want. Unfortunately, I tried to read each file inside the loop but I couldn't. I used txtread ,txtscan but it does not work. – Kh_Ameen Aug 20 '14 at 08:39
  • Dear Dev-iL , I put the code which I used in your comment. I tried to used fopen , tx=textread,textscan to read he data but it does not work. I wonder if you could help me. – Kh_Ameen Aug 20 '14 at 08:53
  • Looks like the original problem of getting the filenames is solved, isn't it? As for textread\textscan, that would be a different question, consider posting it separately. – Dev-iL Aug 20 '14 at 08:57
0
 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
Kh_Ameen
  • 97
  • 10