MATLAB: GUI selecting multiple files with same amount of digits in filename

digitsedittextfilesguipopupmenuuser interface

Hello everyone!
A GUI that I am trying to develop is analysing data from a device. The files with the data that need to be analysed differ in the number of digits to save the files or prefix. For example: ID103215sec.csv or ID0915sec.csv or PreID0392sec.csv. The sec.csv is always fixed. Now I already figured out how to let the user input the prefix 'ID' or 'PreID' and let MATLAB search for files using this prefix and ending in sec.csv by using this code:
function prefix_Callback(hObject, eventdata, handles)
% hObject handle to prefix (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of prefix as text
% str2double(get(hObject,'String')) returns contents of prefix as a double
Prefix = get(handles.prefix, 'String');
Pattern = [Prefix '*'];
handles.Pattern = Pattern;
if isempty(Prefix) == 1;
Prefixval = 0;
else
Prefixval = 1;
end
switch Prefixval
case 1
set(handles.findspecfiles,'Enable','On');
case 0
set(handles.findspecfiles,'Enable','Off');
end
guidata (hObject,handles);
% --- Executes on button press in findspecfiles.
function findspecfiles_Callback(hObject, eventdata, handles)
% hObject handle to findspecfiles (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;
Pattern = handles.Pattern;
filePattern = fullfile(foldername, Pattern);
theFiles = dir(filePattern);
Filenames = struct2cell(theFiles);
Filenames = Filenames(1,1:end)';
Filestringcheck = strfind(Filenames,'sec.csv');
Filestringlogic = cellfun(@isempty,Filestringcheck);
if isempty(Filestringlogic) == 1;
ErrorFileName = msgbox('No files found with input criteria', 'Error','error');
end
Filenames = Filenames(Filestringlogic==0);
set(handles.text7, 'String', Filenames);
Now, if I want the user to use a edittext or popupmenu for the number of digits between the prefix and sec.csv, how can MATLAB find the files with those specific amount of numbers? MATLAB should then be able analyse all the files in the folder with those specific amount of digits.

Best Answer

You could try something like this:
Prefix = 'ID';
Pattern = [Prefix,'*sec.csv']; % better to match both prefix and fixed parts of the filenames.
foldername = '.';
filePattern = fullfile(foldername, Pattern);
S = dir(filePattern);
S = S(~[S.isdir]); % remove folders (probably none, but more robust).
C = {S.name}; % get filenames.
D = cellfun(@nnz,isstrprop(C,'digit')); % count number of digits in names.
N = 4; % requested number of digits.
X = D==N; % index of filenames with that number of digits.
C{X} % show those filenames!
I created three files with the names that you showed in your question, and when I ran the code it showed this:
ans = ID0915sec.csv
Note that this code counts all digits in the filename! If your filename format changes to have multiple groups of digits them you will need to consider an alternative, e.g. a regular expression.