How to classify items in a folder and its subfolder in Matlab to fit purposes
I have a folder dat
that can contain subfolders n
and they contain various .dat
files
I need to get all the files in this subfolder stored in the data structure myarchive
that contains file_name
, it subfolder_name
and the object resulanalysis
that is the result of my script
The purpose of this operation is to get file_name
and subfolder_name
records in myarchive
that match the generalresult
With this code, I can get the entire analysis result of the files contained in the current folder, and I have the corresponding function, but I do not know how to solve the described classification problem.
files = dir('*.dat');
for file = files'
im = load(file.name);
result=myanalyzer(im);
end
Can anyone help me?
If anyone has a better strategy that can satisfy my problem please.
Thank.
source to share
If I understand you correctly, you want, through all the subfolders, to load the .dat file, run some analysis and see if the result matches a certain value. If it is the same as you, you want to store the subfolder and file names in the myarchive data structure. If that's the case, here's the code:
topfolder = '...\dat\'; % Specify the full path to the dat folder
cd(topfolder)
subfolderlist = dir;
subfolderlist = subfolderlist(3:end); % because the first two results are '.' and '..'
counter = 0;
for ii = 1:lenght(subfolderlist)
cd([topfolder,subfolderlist(ii).name])
filename = dir('*.dat');
im = load(filename); % assuming there is only one .dat file in the folder
if myanalyzer(im) == result
counter = counter + 1;
myarchive(counter).subfolder_name = subfolerlist(ii);
myarchive(counter).filename = filename;
end
end
source to share