Asking for filename of excel sheet then using xlsread to display

5 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 20 Mai 2020
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?

Weitere Antworten (1)

Ameer Hamza
Ameer Hamza am 20 Mai 2020
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

Community Treasure Hunt

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

Start Hunting!

Translated by