"Warning: Name is nonexistent or not a directory: " Error Message if I enter the path of a file I want to import

I've written a program in which the path of an excel file I want to import is pasted, converted to a string. The values of the excel file are then imported as "numdata" and "textdata", which are used to do the subsequent calculations.
datapath = input('Please paste path of datafile: ')
pathstring = string(datapath)
path(path,pathstring)
[numdata, textdata] = xlsread(pathstring)
The numerical data and the descriptionas text is successfully , but I receive the following error message everytime I run the script
Warning: Name is nonexistent or not a directory:
How can I improve my script to get rid of this error message?

 Accepted Answer

thepath = '' ; % copy your path here in quotations
filename = '' ; % give your file name in quotations with extension
thefile = fullfile(thepath,filename) ; % copy the path
[num,txt,raw] = xlsread(thefile) ;

4 Comments

KSSV's approach avoids manipulating the MATLAB path at all, which is a Good Thing. Generally, you should only very rarely (if at all) need to manipulate the MATLAB path. It's kind of a heavyweight operation that can have a major impact on the functioning of your MATLAB session.
If I run your code
%Script named Try1
thepath = '"C:\Users\joellukas.affolter\Documents\MATLAB\Try1.xlsx"' ; % copy your path here in quotations
filename = 'Try1.xlsx' ; % give your file name in quotations with extension
thefile = fullfile(thepath,filename) ; % copy the path
[num,txt,raw] = xlsread(thefile) ;
I get the error message
>> try1
Error using xlsread (line 136)
XLSREAD unable to open file '"C:\Users\joellukas.affolter\Documents\MATLAB\Try1.xlsx"\Try1.xlsx'.
File '"C:\Users\joellukas.affolter\Documents\MATLAB\Try1.xlsx"\Try1.xlsx' not found.
Error in try1 (line 5)
[num,txt,raw] = xlsread(thefile) ;
thepath = 'C:\Users\joellukas.affolter\Documents\MATLAB' ; % copy your path here in quotations
filename = 'Try1.xlsx' ; % give your file name in quotations with extension
thefile = fullfile(thepath,filename) ; % copy the path
[num,txt,raw] = xlsread(thefile) ;
Thank you so much! Based on your code I was able to modify mine so that I still can have my path as an input variable without getting an error message.

Sign in to comment.

More Answers (1)

As KSSV shows, the proper way to build a path containing the folder and name of a file you want to import is with fullfile, and if you want to continue to use input, I recommend you accept his answer.
Note that as per Steven's comment, you should not be manipulating matlab's path. All the file IO functions can work directly with the full path of the file and the file doesn't have to be on the matlab path (and actually shouldn't be, separate data from code). So pass the path directly to xlsread.
However, forcing the user to paste a path is not a very good UI. That forces them to go outside of your interface to find the file, copy the path, go back to your UI, and paste it. Not very user friendly.
Instead you can let them do that easily from your interface by using uigetfile. As a bonus, you're guaranteed that the path you receive is valid (or that the user cancelled the selection):
[filename, filepath] = uigetfile('*.xls;*.xlsx', 'Select Exel data file');
if isequal(filename, 0)
error('User cancelled file section')
end
[num, txt, raw] = xlsread(fullfile(filepath, filename));

2 Comments

Thanks for your input! I implemented the UI and I must say it is more convenient to use if you don't know where your file is stored. I connected both versions via a (if, elseif) and an input of 1 and 2, as it is faster if you know where your files are stored and need to import similar files from the same location. This way, you can simply change the name of the file (say from Try1.xlsx to Try2.xlsx, which is faster then opening the UI and selecting one file after another.
If you want to import several files from the same location, then a much faster way is to turn 'Multiselect' 'on' in uigetfile. That way the user can select all the files at once. And again, you get the guarantee that the files selected do actually exist. You can be sure that with pasting paths and then editing them, one time the wrong character will be edited and the path will turn out invalid. And since, it doesn't look like you guard against that, the script/function ends with an error.
So:
[filenames, filepath] = uigetfile('*.xls;*.xlsx', 'Select Exel data file(s)');
if isequal(filenames, 0)
error('User cancelled file section')
end
for fileidx = 1:numel(filenames)
actualfile = fullfile(filepath, filenames{fileidx});
try %we know the file is valid by reading it may still fail (because it's not an excel file, the file disappeared, we don't have permissions, etc.)
[num, txt, raw] = xlsread(actualfile);
catch
warning('Failed to read "%s". Skipping file', actualfile);
continue;
end
%do something with data
end
is what I would do (except I'd probably use readtable or readmatrix instead of xlsread).

Sign in to comment.

Categories

Asked:

on 14 Aug 2019

Commented:

on 16 Aug 2019

Community Treasure Hunt

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

Start Hunting!