MATLAB: Error using imread>get_full_filename

algorithmdetectiondspimage processing

I'm running the below code for my algorithm. Everything is working fine but imread() is giving an error in a loop. If I run the command separately it gives the output but in the loop it's showing an error. Could you please help me out?
Trainingpath = 'Destination Foler path';
imagesets = imageDatastore(Trainingpath,'IncludeSubfolders',true,'LabelSource',...
'foldernames');
[traindata,validation] = splitEachLabel(imagesets,0.50,'randomize');
extracted_features = bagOfFeatures(traindata);
Sclassifier = trainImageCategoryClassifier(traindata,extracted_features);
ConfusMatrix = evaluate(Sclassifier,validation);
mean(diag(ConfusMatrix));
imagename = uigetfile({'*.jpg;*.tif;*.png;*.gif','All Image Files';...
'*.*','All Files' });
image = imread(imagename); %?????????????????????
[Label, score] = predict(Sclassifier,image);
prediction = Sclassifier.Labels(Label);
%fprintf(imagename, prediction);
imagename, prediction

Best Answer

image is the name of a built-in function. Do NOT use it as the name of your variable. Try this:
% Have user browse for a file, from a specified "starting folder."
% For convenience in browsing, set a starting folder from which to browse.
startingFolder = Trainingpath; % or 'C:\wherever';
if ~isfolder(startingFolder)
% If that folder doesn't exist, just start in the current folder.
startingFolder = pwd;
end
% Get the name of the file that the user wants to use.
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uigetfile(defaultFileName, 'Select a file');
if baseFileName == 0
% User clicked the Cancel button.
break; % Exit loop
end
fullFileName = fullfile(folder, baseFileName);
fprintf('Reading in "%s"\n', fullFileName);
rgbImage = imread(fullFileName);
fprintf('Predicting "%s"\n', fullFileName);
[Label, score] = predict(Sclassifier, rgbImage);
Related Question