"Warning: Name is nonexistent or not a directory: " Error Message if I enter the path of a file I want to import
조회 수: 4 (최근 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에서 Environment and Settings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!