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

8 visualizaciones (últimos 30 días)
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;

Respuesta aceptada

Walter Roberson
Walter Roberson el 11 de Sept. de 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 comentarios
Walter Roberson
Walter Roberson el 11 de Sept. de 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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Large Files and Big Data en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by