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

1 visualización (últimos 30 días)
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

Respuesta aceptada

Stephen23
Stephen23 el 19 de Abr. de 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.

Más respuestas (0)

Categorías

Más información sobre Entering Commands en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by