- cell: https://www.mathworks.com/help/matlab/ref/cell.html
- num2str: https://www.mathworks.com/help/matlab/ref/num2str.html
Loop for reading of Images
    4 views (last 30 days)
  
       Show older comments
    
Hi friends.
I want to construct a matlab-code that some images, can be read and concluded. I write this code and need to change it in the form of LOOP pattern.
can enyone helpe or guide me?
Type= {['T1'],['T2'],['T3]};
    TType = menu('Choose a  types of weave :',' T1 ',' T2 ',' T3')
    if Type==1
    W=[1 1 1 ; 0 1 0 ; 0 0 1];
    elseif  WeaveType==2
        W=[0 0 1 ; 0 0 0 ; 1 0 0];
    elseif  WeaveType==3
        W=[0 1 1 ; 1 1 0 ; 1 0 1];
    end
    A1=imread('C:\Users\User\My Documents\MATLAB\1\new1.png');
    A2=imread('C:\Users\User\My Documents\MATLAB\1\new2.png'); 
    A3=imread('C:\Users\User\My Documents\MATLAB\1\new3.png'); 
    A4=imread('C:\Users\User\My Documents\MATLAB\1\new4.png');
    A5=imread('C:\Users\User\My Documents\MATLAB\1\new5.png');
    A6=imread('C:\Users\User\My Documents\MATLAB\1\new6.png');
    A7=imread('C:\Users\User\My Documents\MATLAB\1\new7.png');
    A8=imread('C:\Users\User\My Documents\MATLAB\1\new8.png');
    A9=imread('C:\Users\User\My Documents\MATLAB\1\new9.png');
    B1=imread('C:\Users\User\My Documents\MATLAB\2\new1.png');
    B2=imread('C:\Users\User\My Documents\MATLAB\2\new2.png'); 
    B3=imread('C:\Users\User\My Documents\MATLAB\2\new3.png'); 
    B4=imread('C:\Users\User\My Documents\MATLAB\2\new4.png');
    B5=imread('C:\Users\User\My Documents\MATLAB\2\new5.png');
    B6=imread('C:\Users\User\My Documents\MATLAB\2\new6.png');
    B7=imread('C:\Users\User\My Documents\MATLAB\2\new7.png');
    B8=imread('C:\Users\User\My Documents\MATLAB\2\new8.png');
    B9=imread('C:\Users\User\My Documents\MATLAB\2\new9.png');
    if W(1,1)==0 
        T1=imrotate(A1,90);
    else T1=B1;
    end
    if W(1,2)==0 
        T2=imrotate(A2,90);
    else T2=B2;
    end
    if W(1,3)==0 
        T3=imrotate(A3,90);
    else T3=B3;
    end
    if W(2,1)==0 
        T4=imrotate(A4,90);
    else T4=B4;
    end
    if W(2,2)==0 
        T5=imrotate(A5,90);
    else T5=B5;
    end
    if W(2,3)==0 
        T6=imrotate(A6,90);
    else T6=B6;
    end
    if W(3,1)==0 
        T7=imrotate(A7,90);
    else T7=B7;
    end
    if W(3,2)==0 
        T8=imrotate(A8,90);
    else T8=B8;
    end
    if W(3,3)==0 
        T9=imrotate(A9,90);
    else T9=B9;
    end 
    Final=[T1 T2 T3 ; T4 T5 T6 ; T7 T8 T9];
    imshow(Final);
0 Comments
Answers (1)
  Aastha
 on 12 Jun 2025
        I understand you are looking to optimize your MATLAB code and restructure it using loops. You may refer to the following steps to achieve this: 
1. In the existing code, the image file paths for the variables "A" and "B" follow a pattern from "1/new1", "1/new2", ..., "1/new9" and similarly "2/new1", ..., "2/new9". 
2. You can use the "cell" function in MATLAB to create a cell array of size 9. Then, use string concatenation along with the "num2str" function inside a for loop to dynamically read and store the "A" and "B" images. Below is a sample MATLAB code snippet demonstrating this:
A = cell(9,1);
for i = 1:9
   A{i} = imread(['C:\Users\User\My Documents\MATLAB\1\new' num2str(i) '.png']);
end
For more information on the "cell" and "num2str" functions, please refer to the following MathWorks documentation links: 
I hope this helps!
1 Comment
  Walter Roberson
      
      
 on 12 Jun 2025
				
      Edited: Walter Roberson
      
      
 on 12 Jun 2025
  
			These days you can use
   A{i} = imread("C:\Users\User\My Documents\MATLAB\1\new" + i + ".png");
but I suggest
   projectdir = "C:\Users\User\My Documents\MATLAB";
   %..
   pd1 = fullfile(projectdir, "1");
   %...
   A{i} = imread( fullfile(pd1, "new" + i + ".png") );
See Also
Categories
				Find more on Convert Image Type 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!

