file path error in matlab

73 ビュー (過去 30 日間)
ali hassan
ali hassan 2022 年 1 月 26 日
コメント済み: Image Analyst 2022 年 1 月 27 日
when i am running a command in matlab, it says:
>> a=readtable('111..txt','PreserveVariableNames',true)
Error using readtable
Unable to find or open '111..txt'. Check the path and filename or file permissions.
but once i change the path, problem is resolved. can my code not work with any path? because i am making a GUI and i want it to run the command,nomatter where the path is

採用された回答

Image Analyst
Image Analyst 2022 年 1 月 26 日
It will work with any path as long as that file exists, which yours does not. Are you sure it has two dots in it? That would be unusual. Try this:
fullFileName = fullfile(pwd, '111..txt')
if ~isfile(fullFileName)
warningMessage = sprintf('File not found:\n%s\n\nOn the next screen, select a file.', fullFileName)
uiwait(warndlg(warningMessage))
% Have user browse for a file, from a specified "starting folder."
% For convenience in browsing, set a starting folder from which to browse.
startingFolder = pwd; % or 'C:\wherever';
if ~isfolder(startingFolder)
% If that folder doesn't exist, just start in the current folder.
startingFolder = pwd;
end
% Get the name of the file that the user wants to use.
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uigetfile(defaultFileName, 'Select a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
fullFileName = fullfile(folder, baseFileName)
end
t = readtable('111..txt','PreserveVariableNames',true)
  2 件のコメント
ali hassan
ali hassan 2022 年 1 月 27 日
i tried this but it is giving this error.
Error using readtable (line 318)
Unable to find or open '111..txt'. Check the path and filename or file permissions.
Image Analyst
Image Analyst 2022 年 1 月 27 日
Sorry, it should use fullFileName in readtable(), not the hard coded filename.
fullFileName = fullfile(pwd, '111..txt')
if ~isfile(fullFileName)
warningMessage = sprintf('File not found:\n%s\n\nOn the next screen, select a file.', fullFileName)
uiwait(warndlg(warningMessage))
% Have user browse for a file, from a specified "starting folder."
% For convenience in browsing, set a starting folder from which to browse.
startingFolder = pwd; % or 'C:\wherever';
if ~isfolder(startingFolder)
% If that folder doesn't exist, just start in the current folder.
startingFolder = pwd;
end
% Get the name of the file that the user wants to use.
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uigetfile(defaultFileName, 'Select a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
fullFileName = fullfile(folder, baseFileName)
end
t = readtable(fullFileName,'PreserveVariableNames',true)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSearch Path についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by