How to load an image to a figure using uigetfile/imread?

M
Muhammad · Sep 30, 2020 · 2.3K views
Question
Novice coder here with a question that I am certain there will be an easy answer to - it just escapes me at the moment... I am simply trying to display an image that I select from my PC's location, in a figure for further analysis. I actually have the analysis code working, but the problem I am having is that when I use uigetfile to provide the 'file look-up', I can navigate to the file, get the figure to launch, but the image then doesn't write to the figure. My code thus far is:   [filename, pathname] = uigetfile(... {'*.jpg; *.JPG; *.jpeg; *.JPEG; *.img; *.IMG; *.tif; *.TIF; *.tiff, *.TIFF','Supported Files (*.jpg,*.img,*.tiff,)'; ... '*.jpg','jpg Files (*.jpg)';... '*.JPG','JPG Files (*.JPG)';... '*.jpeg','jpeg Files (*.jpeg)';... '*.JPEG','JPEG Files (*.JPEG)';... '*.img','img Files (*.img)';... '*.IMG','IMG Files (*.IMG)';... '*.tif','tif Files (*.tif)';... '*.TIF','TIF Files (*.TIF)';... '*.tiff','tiff Files (*.tiff)';... '*.TIFF','TIFF Files (*.TIFF)'},... 'MultiSelect', 'on'); % Error check - if no filename there is an error if isequal(filename,0) error(' Load Error: No files selected! Load cancelled.') else end % launch the figure box figure imagefilename = imgfile(filename,pathname); c = imread(imagefilename); image(c) axis image grid on I'm certain the problem is the bit after I launch the figure, but I have tried several different combinations of syntax, but just cant get it right.
Expert Answer
Profile picture of Kshitij Singh
Kshitij Singh PhD Expert
Answered Aug 11, 2026

I think you want fullfile() instead of imgfile().

% Have user browse for a file, from a specified "starting folder."
% For convenience in browsing, set a starting folder from which to browse.
startingFolder = 'C:\Program Files\MATLAB';
if ~exist(startingFolder, 'dir')
  % 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.
  return;
end
fullFileName = fullfile(folder, baseFileName)
myImage = imread(fullFileName);
imshow(myImage);
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!