file path error in matlab
41 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
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 件のコメント
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 Exchange で Environment and Settings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!