Search for a file using edit text with a part of the file name
2 views (last 30 days)
Show older comments
Hello everyone,
Does anybody know how to find a file in a folder with a certain part of the file name? So whether someone's leaves out the extension or a part of it, it will still find the correct file? Now I have this code that allows the user to find a file in 'folder name', but the user would have to enter the entire filename instead of part of it. I have seen things like dir(.) but I cannot seem to make it work
Thanks for the help!
% --- Executes on button press in findfile1.
function findfile1_Callback(hObject, eventdata, handles)
% hObject handle to findfile1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
foldername = handles.foldername;
files = dir(fullfile(foldername, '*.csv'));
files = {files.name}';
for k = 1: length(files);
fname = fullfile(foldername,files{k});
end
filename = get(handles.edit1, 'String');
filename = char(filename);
if 2 == exist(fullfile(foldername,filename), 'file')
%csvread(files);
Filename = sprintf('file exists:\n%s', filename);
uiwait (msgbox(Filename));
else
warningMessage = sprintf ('Warning: file does not exist:\n%s' , filename);
uiwait(msgbox(warningMessage));
end
5 Comments
Stephen23
on 16 Apr 2018
"then it would already be enough to specify the file"
Not always. If a user enters a number ending in 15 then this could be ambiguous, for example
3415 % user enters this
ID123415sec.csv % filename match
dpb
on 16 Apr 2018
The Win directory pattern-matching is pretty feeble; it wouldn't be perfect but you could pass
d=dir('ID*15sec.csv');
and retrieve all files with whatever the other four (or N) digits are. Unfortunately, while Win implements the '?' wildcard matching a single character, MATLAB doesn't pass it through to the builtin dir; you'd have to shell to the OS for
d=dir('ID????15sec.csv');
to be effective.
Accepted Answer
Stephen23
on 16 Apr 2018
Edited: Stephen23
on 17 Apr 2018
You could experiment with dir:
str = '0283'; fmt = sprintf('*%s*.csv',str); S = dir(fmt); filename = S.name
Or perhaps:
fmt = sprint('ID%s15sec.csv',str);
One flexible solution would be to let the user define as much of the filename as they want, find all matches, and then filter for only those that match the required filename pattern:
str = '0283'; S = dir(sprintf('*%s*',str)); N = {S(~[S.isdir]).name}; idx = ~cellfun('isempty',regexp(N,'^ID\d{4}15sec\.csv$')); filename = N{idx}
Note:
- you will need to add the filepath.
- you will need to add checks to see if zero/multiple files match what the user gives. If you omit this it will only cause problems later!
As an alternative, you could simply use uigetfile.
16 Comments
More Answers (0)
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!