ImageDatastore from text file

R
riyanjain · May 5, 2021 · 1.9K views
Question
Hi I need to classify some image data using the trained resnet network. For the testing data, I need to call them using text file which consist from a sequence directory such as follows: testingdata.txt: /DataSet/label1/1.jpg /DataSet/label1/2.jpg /DataSet/label1/3.jpg /DataSet/label1/4.jpg /DataSet/label2/1.jpg /DataSet/label2/2.jpg /DataSet/label2/3.jpg /DataSet/label2/4.jpg How to read those image dataset directory from text file, to be used as testing data for deep learning classification.?
Expert Answer
Profile picture of John Williams
John Williams PhD Expert
Answered Aug 24, 2026
Try this code snippet. The below code will read images, whose locations are specified in the testingdata.txt file and will also label them according to their foldernames.
 
 
% Read the filenames and put them in cell array
fId = fopen('testingdata.txt');
tline = fgetl(fId);
tlines = cell(0, 1);
while ischar(tline)
    tlines{end+1, 1} = (tline);
    tline = fgetl(fId);
end
% Create an imageDatastore object
imds = imageDatastore(tlines, 'LabelSource', 'foldernames');
100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!