Pipeline to access a folder and the subfolders in it until it can retrieve the data

2 Ansichten (letzte 30 Tage)
I have a main folder (eg: Patients) and following their are multiple subfolders, I have to open a specific subfolder out of all the subfolders and so forth until it reaches the data set to retrieve it. . Eg: Patients --> Patient_01 (until patient x) --> MRI --> pre_data --> DICOMS --> [DATA]. Within each subfolder is another subfolder. I know that I will need an if and for loop as there is also a pre data set and a post data set.
If you could help me or guide me on how I could solve this problem. Thank you in advance

Antworten (1)

Suraj
Suraj am 22 Mai 2023
Greetings Samadhi,
It is my understanding that you want to search through all the subfolders of a main folder, and for all DICOM files found, perform some analysis on the data.
You can use "Recursive directory listing - Enhanced RDIR" from the File Exchange to accomplish this. Following is a sample code snippet that demonstrates how to perform a depth-first search through the subfolders of a main folder and loop through DICOM files in each subfolder:
% Define path of the top-level folder
rootFolder = 'C:\Users\JohnDoe\Documents\Patients';
% Perform a depth-first search through the subfolders of the top-level folder
fileList = rdir(fullfile(rootFolder,'**','*.dcm'));
% Loop through each DICOM file found in the subfolders
for i = 1:length(fileList)
% Get the full path of the DICOM file
dicomPath = fileList(i).name;
% Read the DICOM file
dicomData = dicomread(dicomPath);
% Perform some analysis on the DICOM data
% ...
end
In this code snippet, we first define the path to the top-level folder. We then use the rdir function to perform a depth-first search through all the subfolders of the top-level folder and retrieve a list of all DICOM files found (including those in subfolders). We then loop through each DICOM file in the list and read the data using the dicomread function. Finally, we perform some analysis on the DICOM data (which you can replace with your own analysis code).
You can find more information and examples of using these functions in the following links:
Hope this helps!
Regards,
Suraj
  1 Kommentar
Stephen23
Stephen23 am 22 Mai 2023
Bearbeitet: Stephen23 am 22 Mai 2023
This answer is very outdated: since R2016b MATLAB's inbuilt DIR has also supported recursive directory searches:
The provided code is also incorrectly commented and contains bugs: for example, this comment
% Get the full path of the DICOM file
is not correct because the following line only returns the filename by itself, not the full path as the comment incorrectly states. We can fix those bugs and also avoid any third-party functions:
P = 'C:\Users\JohnDoe\Documents\Patients';
S = dir(fullfile(P,'**','*.dcm')); % no third-party functions required
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name), % the actual full path
D = dicomread(F);
.. etc
end
You know that TMW has badly publicised an improvement when many years later TMW staff are totally unaware of the improvement. Reading the DIR documentation is highly recommended.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu File Operations finden Sie in Help Center und File Exchange

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by