フィルターのクリア

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

5 ビュー (過去 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
chihyu
chihyu 2021 年 9 月 11 日
Thank you both, I made it

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLarge 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