Analyse files in a different folder than current folder

2 ビュー (過去 30 日間)
Debbie Oomen
Debbie Oomen 2018 年 4 月 24 日
編集済み: Stephen23 2018 年 4 月 24 日
Hi all!
For my GUI that analyses data files, the scripts are in the current folder. The data files that need to be analysed are in a separate folder. This folder is selected by the user beforehand using uigetdir. However, when I want another callback to find a specific file in that folder, it gives me an error because it is not searching in the right folder.
foldername = handles.foldername; %gets foldername from Choosefolder_Callback
str = char(get(handles.edit1, 'String')); %gets user input from edit1 textbox
S = dir(sprintf('*%s*',str))); %finds filename that matches user input
N = {S(~[S.isdir]).name}; %puts filenames in cell array
idx = ~cellfun('isempty',regexp(N,'^ID\w\d+sec\.csv$')); %indices of filenames which match regular expression
if nnz(idx)==1 % ensures the 1 filename match
filename = N{idx}; %get filename
indivtxt = sprintf('File has been located');
set(handles.text7, 'String', indivtxt); %sets string to individual data file selected
else
warningMessage = sprintf ('Warning: file does not exist\n%s');
uiwait(msgbox(warningMessage, 'Error', 'error'));
end
Why does this only work when I select the data files folder as my current folder and how can I change this? I need the current folder to be the scripts that I need for my GUI

採用された回答

Stephen23
Stephen23 2018 年 4 月 24 日
編集済み: Stephen23 2018 年 4 月 24 日
You need to give dir the folder path, otherwise it will just look in the current directory:
foldername = handles.foldername;
str = char(get(handles.edit1, 'String'));
pat = sprintf('*%s*',str);
S = dir(fullfile(foldername,pat));
You need to use foldername every time you want to access those files, e.g. using dir, file reading, file writing, etc. Note that you were using the folder path and fullfile in your earlier questions:
So why did you suddenly stop using the folder path?

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by