"Warning: Name is nonexistent or not a directory: " Error Message if I enter the path of a file I want to import
1 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
KSSV
2019-8-14
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 个评论
KSSV
2019-8-15
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) ;
更多回答(1 个)
Guillaume
2019-8-16
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 个评论
Guillaume
2019-8-16
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).
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!