How to convert FileName(char) to the variable name and display the variable name image

7 views (last 30 days)
I want to use uigetfile to call my picture file instead of using cd. But there is a problem, that is, FileName belongs to char.
Is there a way to convert char to the variable name, and then display the image.
clc; clear
if exist('PathName','var')
if PathName ~= 0
else
PathName = 'c:\';
end
else
PathName = 'c:\';
end
[FileName, PathName] = uigetfile( ...
{'*.jpg;*.tif;*.png;*.gif','All Image Files';...
'*.*','All Files' },'MultiSelect', 'on');
load(strcat(PathName,FileName));
varname = genvarname(PathName)
I = imread(varname);
imshow(I)
axis on;

Accepted Answer

Walter Roberson
Walter Roberson on 11 Sep 2021
[FileName, PathName] = uigetfile( ...
{'*.jpg;*.tif;*.png;*.gif','All Image Files';...
'*.*','All Files' },'MultiSelect', 'on');
if ~ischar(FileName) && ~iscell(FileName); return; end %user cancel
FileName = cellstr(FileName); %if user selected only 1 then it is char
fullname = fullfile(PathName, FileName);
I = imread(fullname);
[~, basename] = fileparts(fullname);
imshow(I)
axis on;
title(basename);
  4 Comments
Walter Roberson
Walter Roberson on 11 Sep 2021
[FileName, PathName] = uigetfile( ...
{'*.jpg;*.tif;*.png;*.gif','All Image Files';...
'*.*','All Files' },'MultiSelect', 'on');
if ~ischar(FileName) && ~iscell(FileName); return; end %user cancel
FileNames = cellstr(FileName); %if user selected only 1 then it is char
FileNames = fullfile(PathName, FileNames);
numfiles = length(FileNames);
for K = 1 : numfiles
fullname = FileNames{K};
I = imread(fullname);
[~, basename] = fileparts(fullname);
imshow(I)
axis on;
title(basename);
pause(0.5)
end

Sign in to comment.

More Answers (0)

Categories

Find more on Large Files and Big Data 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!