How to detect which type of data user selected 'text' or 'excel' file via "uigetfile" and then perform further code?

4 次查看(过去 30 天)
i have written a code that allows user to input data and give both options of "text" and "Excel"..that is
[filename, pathname] = uigetfile({'*.xlsx','Excel Files(*.xlsx)'; '*.txt','Txt Files(*.txt)'}, 'Pick a file');
if '*.xlsx' % if excel file select
disp('User Selected Excel File.');
else
disp('User Selected Text File.');
end
% i tried isfile function but is detects data type.. kindly Guide. Thanks

采纳的回答

Cris LaPierre
Cris LaPierre 2021-2-19
Use this syntax of uigetfile
Here, indx corresponds to what filter was selected (see here). If that doesn't work, you can implement your own check using fileparts.
Here, ext will be the file extension of filename.
  3 个评论
Cris LaPierre
Cris LaPierre 2021-2-20
编辑:Cris LaPierre 2021-2-21
I understand what you are trying to do, but you have not coded it correctly. For example
if '*.xlsx' % if excel file select
does nothing right now because you have not correctly set up the conditional statement.
I suggest going through Ch 13 of MATLAB Onramp, which will introduce you to if statements. Perhaps that will help you see how to use what I shared to obtain what you want.
taimour sadiq
taimour sadiq 2021-2-21
编辑:taimour sadiq 2021-2-21
Dear Thanks More than Thanks for ur Help, which Finally lead me to achieve my goal.. So nice of You.. Sharing the Working Code and code file attached also..
clc; clear all; close all;
BaseDir = 'C:\Program Files\Matlab\bin\';
[filename, pathname] = uigetfile({'*.xlsx','Excel Files(*.xlsx)';...
'*.txt','Txt Files(*.txt)'}, 'Pick a file', BaseDir);
if isequal(filename, 0)
disp('User aborted file choosing.');
return;
else
[fPath, fName, fExt] = fileparts(filename);
switch lower(fExt)
case '.xlsx'
disp = 'User Selected Excell Data'
case '.txt'
disp = 'User Selected Text Data'
otherwise % Under all circumstances SWITCH gets an OTHERWISE!
error('Unexpected file extension: %s', fExt);
end
end

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 String Parsing 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by