Radi_tech’s blog

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

【MATLAB】Save images with sequential numbers according to the variables in the for method

When saving images, it is easier to manage them if they are sequentially numbered.

ex) 001.jpg, 002.jpg, 003.jpg,.........100.jpg

%Assume jpg files of an MRI with multiple images
for x = 1:length(MR_fl)
    
    img    = imread(fullfile(main_fd,MR_fl(x)));
    
    %make save file path
    %'%03i' is the code to create sequential number
    save_fl= strcat(num2str(x,'%03i'),'.jpg');
    
    %make full path
    save_path = fullfile(save_fd,save_fl);
    
    %Saving img with optimal color map (Color map is  [])
    imwrite(img,[],save_path)    
end    

if you want to get 4 digits, just write '%04i'.f:id:radi_tech:20211011184740p:plain