Effacer les filtres
Effacer les filtres

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

5 vues (au cours des 30 derniers jours)
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;

Réponse acceptée

Walter Roberson
Walter Roberson le 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 commentaires
Walter Roberson
Walter Roberson le 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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Large Files and Big Data dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by