Just because something was written by someone at a university does not mean that it is well written. That code is pointlessly complicated, and has several strange "features":
- using dir to get all directory contents, and then later using if-s to pick non-folders and filenames with .txt extension. What a pointlessly complicated piece of code: you should not learn from this. Much simpler: call dir with the correct match string, including wildcard and file extension.
- no preallocation on the output array Mfiles.
- uses obsolete textread, which the documentation clearly recommends against using.
In any case, that code does not actually do what you ask for: your question you state "I have multiple files with the same names in different subfolders. All files are called runoff.txt. The subfolders are called results1, results2, results3 and so on." But that page states (translated) that it gets all files from one folder. So why use that code, which is pointlessly complicated and does not do what you want anyway?
All beginners would be much better off learning MATLAB from the MATLAB documentation, rather than relying on code written by some random academic who still thinks that cell arrays are very daring, or by some teenager who uploads a youtube blog on some neat tricks that they "discovered". The documentation describes the two basic ways of processing a sequence of files or folders:
and also on this forum:
In particular this example:
"To read all files that match *.jpg with imread:"
jpegFiles = dir('*.jpg');
numfiles = length(jpegFiles);
mydata = cell(1, numfiles);
for k = 1:numfiles
mydata{k} = imread(jpegFiles(k).name);
end
You can easily adapt this to read the same file from multiple directories, perhaps something like this (untested):
F = 'name.txt';
D = '.';
S = dir(fullfile(D,'results*'));
X = [S.isdir] & ~ismember({S.name},{'.','..'});
N = {S(X).name};
C = cell(size(N));
for k = 1:numel(N)
T = fullfile(D,N{k},F);
C{k} = textscan(T,...
end
If you have sequentially numbered files then you might also be interested in processing them in numeric order, in which case you might like to download my FEX submission natsortfiles:
and then you can use it on this line:
N = natsortfiles({S(X).name});
Best Answer