Clear Filters
Clear Filters

how to find files in a directory with a list of strings froma a matrix

2 views (last 30 days)
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

Accepted Answer

Stephen23
Stephen23 on 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.

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!