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

1 view (last 30 days)
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

KSSV
KSSV on 14 Aug 2019
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
KSSV on 15 Aug 2019
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) ;
Joel Affolter
Joel Affolter on 16 Aug 2019
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)

Guillaume
Guillaume on 16 Aug 2019
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
Joel Affolter
Joel Affolter on 16 Aug 2019
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.
Guillaume
Guillaume on 16 Aug 2019
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

Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!