Searching Folder for Variable File Name
26 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Brandon Lange
am 24 Aug. 2021
Kommentiert: Stephen23
am 25 Aug. 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#1 Kommentar
Akzeptierte Antwort
Jan
am 24 Aug. 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 Kommentare
Jan
am 25 Aug. 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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu File Operations 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!