"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?

採用された回答

KSSV
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
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) ;
Joel Affolter
Joel Affolter 2019 年 8 月 16 日
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.

サインインしてコメントする。

その他の回答 (1 件)

Guillaume
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 件のコメント
Joel Affolter
Joel Affolter 2019 年 8 月 16 日
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 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 ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by