Determine the type of input in a function??

is there a way to determine the type of input in a function?? For example,
Case 1: if input is a .mat file called 'input.mat' somefunction('input.mat') would recognize that the input is a .mat file
Case 2: if input is a mxn matrix variable called 'input' somefunction(input) would recognize the input as a matlab variable
Case 3: if input is an excel file .xlsx called 'input.xlsx' somefunction('input.xlsx') would recognize the input as an excel file.

 採用された回答

Matt Fig
Matt Fig 2012 年 11 月 6 日
編集済み: Matt Fig 2012 年 11 月 6 日

0 投票

Well you can't really pass a .mat file or excel file to a MATLAB function. You can pass a string name of a file. So something like this would work:
function []=getinput(A)
if ischar(A)
if strfind(A,'.mat')
disp('A .mat file name')
elseif strfind(A,'.xls')
disp('A .xls file name')
else
disp('Unknown file type')
end
else
disp(['You passed a ',class(A)])
end
Now we can test it out from the command line:
>> getinput('myfile.xls')
A .xls file name
>> getinput('myfile.xlsx')
A .xls file name
>> getinput('myfile.mat')
A .mat file name
>> getinput('myfile.m')
Unknown file type
>> getinput(magic(3))
You passed a double
>> getinput(true(2))
You passed a logical

3 件のコメント

S H
S H 2012 年 11 月 6 日
Matt, thanks for the help. I was thinking something along the lines of the function "isa" but this will certainly do the trick! Thanks!
Walter Roberson
Walter Roberson 2012 年 11 月 6 日
Watch out for 'input.mat.xls' which is a .xls file but the code above would detect as being .mat
Daniel Shub
Daniel Shub 2012 年 11 月 7 日
You might want to use FILEPARTS instead of STRFIND.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Type Identification についてさらに検索

タグ

質問済み:

S H
2012 年 11 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by