views:

80

answers:

2

how I can read the following files using the for loop: (can the loop ignore the characters in filenames?)

abc-1.TXT
cde-2.TXT
ser-3.TXT
wsz-4.TXT
aqz-5.TXT
iop-6.TXT

What do I have to add at the beginning of this loop ??

for i = 1:1:6  
    nom_fichier = strcat(['MyFile\.......' num2str(i) '.TXT']);
+3  A: 

You can avoid constructing the filenames by using the DIR command. For instance:

myfiles = dir('*.txt');
for i = 1:length(myfiles)
    nom_fichier = myfiles(i).name;
    ...do processing here...
end
mtrw
+1  A: 

First of all, why would you use strcat here? This is, by itself, a SINGLE string. All concatenation has already been done by the brackets [].

['MyFile\.......' num2str(i) '.TXT']

Next, I'm not certain what is your question here. Is it how to load in the data? If the files are simply delimited numbers, with the same number of them on each line, then load will suffice to load them in, or perhaps you may need textread.

My guess is you do not know how to build the main part of of the file name. You might do it this way:

Names = {'abc' 'cde 'ser' 'wsz' 'aqz' 'iop'};
for i = 1:6
  fn = ['MyFile',filesep,Names{i},'-',num2str(i),'.TXT'];
  data = load(fn);

  % do other stuff ...

end

If you don't want to create a variable with the names by typing them in, then use dir, perhaps like this to create a list of text file names:

Names = dir('MyFile\*.TXT');
woodchips

related questions