Use of dir - too slow!
Show older comments
I have a main folder on a network containing a lot of subfolders(~1000), each subfolder has ~1000 DICOM files as well. My code needs to find a string in the DICOM header fields. All the files for each subfolder will have the same field so I only need to compare a file of each subfolder...but the problem I find is that for each subfolder I have to user the dir command and that is time consuming.
My code is:
all_folders=dir(path_browse); %struct containing every folder
no_folders=length(all_folders)-2; %number of folders, excluding '.' and '..'
for i=1:no_folders
name_folder=all_folders(i+2).name; %subfolder to find match
aux_dir=dir(name_folder); %files in subfolder
cd(name_folder) %moves to subfolder
test_file=dicominfo(aux_dir(3,1).name); %DICOM header from first file in the folder
search_field(i)=strcmp(lower(test_file.field),field_query); %compare fields
cd(path_browse) %back to main folder
end
Then I would just need to find the 1s in search_field. Is there any option to open a file without using dir or ls? The code works but I want it to be more efficient.
Regards,
Sergio
1 Comment
"I have to user the dir command and that is time consuming."
How do you know that dir is the bottleneck? I can see two cd calls in that code: cd makes debugging harder and is slower than using relative/absolute filepaths.
"Is there any option to open a file without using dir or ls"
It is not required to use dir or ls before opening a file: it is also possible to generate filenames from some sequence. Which method to use depends on those filenames, and how much you know about them. Read the MATLAB documentation to know more:
"The code works but I want it to be more efficient."
Then get rid of cd by using absolute/relative paths, and run the profiler so that you can show us which lines are taking the most time.
Accepted Answer
More Answers (0)
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!