how to find files in a directory with a list of strings froma a matrix
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
liu James
am 19 Apr. 2017
Beantwortet: Stephen23
am 19 Apr. 2017
I have a matrix with a few list of characters and I'm trying to find the files with similar name in a directory to load. I can't get this code to work correctly.
u = {'IC','IH'};
myFolder = 'C:\Users\Desktop\Jar'
filePattern = fullfile(myFolder, 'u');
matFiles = dir(filePattern)
for k = 1:length(matFiles)
baseFileName = matFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
matData(k) = load(fullFileName);
end
0 Kommentare
Akzeptierte Antwort
Stephen23
am 19 Apr. 2017
Have a look at the output of this operation:
>> myFolder = 'C:\Users\Desktop\Jar';
>> filePattern = fullfile(myFolder, 'u')
filePattern =
C:\Users\Desktop\Jar\u
You joined the string 'u' onto the end of the filePattern, as if it were a foldername. Is this the effect that you desired?
If you want to use both of the strings in the cell array u, then you will have to do them one-at-a-time in a loop, and merge the results together.
Also note that you need to learn how to debug. Many beginners write code and rely only on what they imagine/wishfor/want/believe/assure/... about what their code is doing. But code does not care what is in your head, and it is your job to check what the code is actually doing. In your case it is easy because you only have a few operations and the string outputs can be checked quit trivially by hand. Always check what you code is actually doing, do not rely on what you think it is doing.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!