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

조회 수: 8 (최근 30일)
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;

채택된 답변

Walter Roberson
Walter Roberson 2021년 9월 11일
[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
Walter Roberson
Walter Roberson 2021년 9월 11일
[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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by