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

2 次查看(过去 30 天)
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

采纳的回答

Stephen23
Stephen23 2017-4-19
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 个)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by