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

前端 未结 2 1289
迷失自我
迷失自我 2021-01-27 20:35

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

相关标签:
2条回答
  • 2021-01-27 21:16

    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
    
    0 讨论(0)
  • 2021-01-27 21:22

    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');
    
    0 讨论(0)
提交回复
热议问题