Help with my image loading algorithm

2 views (last 30 days)
Sandipa Sharma
Sandipa Sharma on 3 Aug 2018
Answered: Image Analyst on 3 Aug 2018
I'm trying to create an algorithm to load multiple images from 3 folders which also contain subfolders where my images are organized into sets (Left/Middle/Right angles). This function is creating a cell matrix with nSubjects x 3*nsets. The first index refers to the number of the subject I want, and the second index is the picture that I want to load considering the images are stored consequently.
The following code I've been working off of keeps giving me an error that I am not seeing how to fix (if it is a syntax error or how I saved my images, I am a beginner with Matlab):
fold1 = '\Subject';
fold2 = '\subject';
fold3 = ["_Left", "_Middle", "_Right"];
for i = 1:nSubject
for j = 1:nSets
if(j == 1)
cnt = 1;
elseif (j == 2)
cnt = 4;
elseif (j == 3)
cnt = 7;
elseif (j == 4)
cnt = 10;
end
for k = 1:length(fold3)
folder = [basePath fold1 num2str(i,'%2d') fold2 num2str(i,'%2d') convertStringsToChars(fold3(k))];
I = dir(fullfile(folder,'*.jpg'));
filename = fullfile(folder,I(j).name);
IM{i,cnt} = imread(filename);
cnt = cnt + 1;
end
end
end
When I run this, I get an error with line "filename = fullfile(folder,I(j).name);" and I have no idea why. Hope that was enough information, appreciate your help!
  1 Comment
Rik
Rik on 3 Aug 2018
What was the full error message (i.e. all text in red)?

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 3 Aug 2018
The number of files in the folder has no relation to the loop iterator you (unwisely) called j. Why are you doing I(j).name?
Also, why are you reading all your images into a cell array called IM? Another bad idea in my opinion. You may soon run out of memory. Just process the images inside the loop. Don't store them all just to process them later with a second loop. Process them right then and there and re-use the variable and save a ton of memory space.

Community Treasure Hunt

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

Start Hunting!