Searching Folder for Variable File Name

26 vues (au cours des 30 derniers jours)
Brandon Lange
Brandon Lange le 24 Août 2021
Commenté : Stephen23 le 25 Août 2021
I am trying to combine the data from several different files to analyze together. The data as of right now is in individual csv files that are in a common folder. The ultimate goal is to be able to store the information from each file in arrays to be called on when needed. All of the files are name the same with the exception of the group and blade number. It looks something like "Lab-Proc-RPM-G#_B#_Summary". G ranges 1:19 and B ranges 1:4. I have created a for loop to create the individual file names hoping that I could use dir to search the location for these files but when the variable "fileName" is used to dir, it does not use the variable it stores but the variable name to search.
I have tried to convert the string class of fileName to a character but that didn't do anything; I have tried to use just the "G#_B#" portion of the file name to search just for that; I have added the wildcard * in just about everyway I can; i have added this file location to the path in the current folder box
Another weird thing is that if I copy part of the file name created and outputted to the command window and paste it into the search box of my file explorer, it will find the file. If I copy the entire file name that is created, both string and char, to the search box the file is not found. Could this be a class type issue between the output of the fileName variable and the required class in the search bar?
Proc2_500_Dir = "C:\Users\jlange\OneDrive - Rest of Path"
for i = 1:19 %Number of Groups
i_pad = sprintf('%02d',i);
for j = 1:4 %Number of Blades per group
j_pad = sprintf('%02d',j);
fileName = "G" + i_pad + "B" + j_pad
%fileName = "57306-Proc2 - 500 Steps-sec_G" + i_pad + "_B" + j_pad + "_Summary"
fileName = convertStringsToChars(fileName)
dir fileName
end
end
Update: The code was updated to output the fileName as G#_B#

Réponse acceptée

Jan
Jan le 24 Août 2021
Use the functional form:
dir(fileName)
Omitting the parentheses is equivalent to:
dir('fileName')
A simplified version of your code:
Proc2_500_Dir = "C:\Users\jlange\OneDrive - Rest of Path"
for i = 1:19 % Number of Groups
for j = 1:4 % Number of Blades per group
fileName = sprintf('G%02dB%02d', i_pad, j_pad);
dir(fullfile(Proc2_500_Dir, fileName))
end
end
  2 commentaires
Brandon Lange
Brandon Lange le 24 Août 2021
Thank you for this response. I tried using the function form both with and without paranthesis and wildcards.
I ended up working around the dir search and just checked if the file was in the folder and if it was to read it and write it to a 3D matrix. The check may not be strictly necessary as I already know that these files exist in the folder but it started as a sanity check that seems like a "nice to have" feature
Proc2_500_Dir_Info = dir(Proc2_500_Dir);
%% Loops read each file and place each group in a (5,10,4) matrix. The four pages of each matrix indicates
% the blade number for each group. The fifth blade for each
for i = 1:19 %Number of Groups
i_pad = sprintf('%02d',i);
for j = 1:4 %Number of Blades per group
j_pad = sprintf('%02d',j);
ident = "G" + i_pad + "_B_" + j_pad; %blade identifier
fileName = "57306-Proc2 - 500 Steps-sec_G" + i_pad + "_B" + j_pad + "_Summary.csv";
fileNameChar = convertStringsToChars(fileName);
check = any(Proc2_500_Dir_Info(3).name == fileNameChar);
if check == 1
switch i
case 1
G01(:,:,j) = readmatrix(fileName, 'Range', 'B2:K6');
case 2
G02(:,:,j) = readmatrix(fileName, 'Range', 'B2:K6');
case 3
G03(:,:,j) = readmatrix(fileName, 'Range', 'B2:K6');
case 4
G04(:,:,j) = readmatrix(fileName, 'Range', 'B2:K6');
case 5
G05(:,:,j) = readmatrix(fileName, 'Range', 'B2:K6');
case 6
G06(:,:,j) = readmatrix(fileName, 'Range', 'B2:K6');
case 7
G07(:,:,j) = readmatrix(fileName, 'Range', 'B2:K6');
case 8
G08(:,:,j) = readmatrix(fileName, 'Range', 'B2:K6');
case 9
G09(:,:,j) = readmatrix(fileName, 'Range', 'B2:K6');
case 10
G10(:,:,j) = readmatrix(fileName, 'Range', 'B2:K6');
case 11
G11(:,:,j) = readmatrix(fileName, 'Range', 'B2:K6');
case 12
G12(:,:,j) = readmatrix(fileName, 'Range', 'B2:K6');
case 13
G13(:,:,j) = readmatrix(fileName, 'Range', 'B2:K6');
case 14
G14(:,:,j) = readmatrix(fileName, 'Range', 'B2:K6');
case 15
G15(:,:,j) = readmatrix(fileName, 'Range', 'B2:K6');
case 16
G16(:,:,j) = readmatrix(fileName, 'Range', 'B2:K6');
case 17
G17(:,:,j) = readmatrix(fileName, 'Range', 'B2:K6');
case 18
G18(:,:,j) = readmatrix(fileName, 'Range', 'B2:K6');
case 19
G19(:,:,j) = readmatrix(fileName, 'Range', 'B2:K6');
end
end
end
end
Jan
Jan le 25 Août 2021
G = {};
FileList = dir(fullfile(Proc2_500_Dir, '*_Summary.csv'));
for k = 1:numel(FileList)
FileName = FileList(k).name;
File = fullfile(FileList(k).path, FileName);
index = sscanf(FileName, 'G%d_B_%d', 2);
G{index(1)}(:, :, index(2)) = readmatrix(File, 'Range', 'B2:K6');
end
This avoid the hiding of indices in the names of the variables. The later access of the array is much easier tham having a pile of numbered variable names.
Actually this should not work relaibly:
any(Proc2_500_Dir_Info(3).name == fileNameChar)
The == operator performs an elementwise comparison in case of CHAR vectors. In addition you are using the 3rd file of the folder only.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur File Operations dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by