Radi_tech’s blog

Radiological technologist in Japan / MRI / AI / Deep learning / MATLAB / R / Python

【MATLAB】Batch processing of images from multiple cases

In medical image analysis, there are many situations where it is necessary to process multiple images of multiple cases at once.

e.g., 20 MRI images of cases 001-010 each.

Code for continuous processing with for statement.

Procedures

  • Define the main folder
  • List the internal folders in a cell type (can be a list)
  • List files for each internal folder in a cell type (can be a list)
  • Execute a for statement on the list of files

The folder and files structure
MATLAB, for文, MRI

% Specify the fold of Main that contains the case
MR_main_fd= uigetdir();
MR_fd=dir(MR_main_fd);

MR_fd=MR_fd(MR_main_fd); % Process hidden files so that they are not loaded
MR_fd=MR_fd(~ismember({MR_fd.name}, {'. ,'.'. ,'.DS_Store','. _.DS_Store'}));
MR_fd=MR_fd([MR_fd(:).isdir]);

%Convert to Cell type 
MR_fd={MR_fd.name}

In the meantime, we can define each folder to be processed above.
You can then define each folder to be processed by

~
for x= 1:length(MR_fd)
    
    %Get a list of %DICOM files. For other file extensions, change '*.dcm' to jpg, etc.
    MR_dcm_list= dir() to get the list.
    MR_dcm_list= dir(fullfile(MR_main_fd, MR_fd{x}, '*.dcm'));
    
    %↑↑Processing is required if there are hidden files
    
    %↓↓for statement with list of files
    for y= 1:length(MR_dcm_list)
        
        %file path
        MR_dcm_fl= fullfile(MR_main_fd, MR_fd{x},MR_dcm_list(y).name
        
        %We get a path for each file, and we can write the process below.
        
    end
end