Asking for filename of excel sheet then using xlsread to display
3 次查看(过去 30 天)
显示 更早的评论
I'm trying to write a code in which the user is asked to input a filename then use xlsread to display it in the command now
Ive trie using the following code:
A=input('Enter filname','s')
Z=xlsread(A)
But I get this error
XLSREAD unable to open file abc
Error using ==> validpath at 42
File C:\Users\XXXXXX\Documents\ abc.xls not found.
0 个评论
采纳的回答
Walter Roberson
2020-5-20
You need to define the behaviour you want when the user does not enter a valid file name, as in this case.
When you do not provide a file extension, xlsread will assume .xls . If you want to prevent that, and have it attempt to open the name with no extension, then add a '.' on to the end of the name, as in
xlsread([A '.'])
I would suggest you do input validation before you invoke xlsread to ensure that the name the user gave matches an actual file. You need to decide whether you want to force the user to give a full extension, and if not then you need to decide what order of extensions you want to try.
Might I suggest that this problem would be reduced if you were to use uigetfile() instead of input() to get the file names?
0 个评论
更多回答(1 个)
Ameer Hamza
2020-5-20
This error shows that the file abc.xlsx does not exist in MATLAB. Are you writing the correct file name? The xlsread can only read an excel file if it is on the MATLAB path (for example, the current folder). You can add a check so that xlsread only run if the file exists
A = input('Enter filname: ','s')
if exist(A, 'file')
Z=xlsread(A)
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!