GUI selecting multiple files with same amount of digits in filename

2 visualizaciones (últimos 30 días)
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.
  1 comentario
Stephen23
Stephen23 el 24 de Abr. de 2018
Editada: Stephen23 el 24 de Abr. de 2018

Note this complex code:

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

could be simplified to just half the code:

if isempty(Prefix)
    set(handles.findspecfiles,'Enable','Off');
else
    set(handles.findspecfiles,'Enable','On');
end

Why make it more complex than it needs to be? Also note that testing isempty(...)==1 serves no purpose whatsoever: why test a logical value against 1 just to produce another exactly identical logical value? Some beginners like to do this, for reasons that have never been made clear to me.

I would also suggest that your Pattern should include the file extension: [Prefix,'*.csv'], just to make it a tiny bit more robust. Perhaps even [Prefix,'*sec.csv'], then dir already finds only the filenames that match that format, and so you do not need to use that code to filter a larger list of filenames.

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 24 de Abr. de 2018
Editada: Stephen23 el 24 de Abr. de 2018
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.
  20 comentarios
Debbie Oomen
Debbie Oomen el 26 de Abr. de 2018
Editada: Debbie Oomen el 26 de Abr. de 2018
I changed the code the way you've mentioned. The thing is, the filenames are identified by the user by entering the prefix for the wanted files and the number of digits behind the prefix. Without using those three lines of code I get an error. I have attached the code for everything total. So far, the user can input files in three different ways: using an individual file, selecting files based on the identity or using all files in a folder. The loop would be for the specific files callback that allows the user to enter the prefix and digits of the filenames.
dpb
dpb el 26 de Abr. de 2018
Editada: dpb el 27 de Abr. de 2018
Well, as Stephen says, learn to use the debugger and step through and see what breaks and then research why.
Since you don't even tell us what the error is, and the crystal ball is in the shop (yet again :( ); not going to try to guess but I'll put odds on that it has to do with the difference between how the filenames array is being stored; you're recasting from its form as an a dir() structure array to a cell array...I've really no idea what that would look like in the end but it can't be as simple as simply filenames(i).name for the ith filename in the dir() array. ...
ADDENDUM/CORRECTUM I kept being puzzled by what was going on here so had a few minutes and played around some...there were too many similar variable names; my eyes got crossed earlier so there's one less cast than had thought earlier--you didn't convert the handles struct, only the DIR() struct. I then finally realized why you did what you did; but it's not simplest way to go at it: to return the names alone as cellstr array, just write
filenames = {filesfolder.name}.';
which will leave a column vector of the name field from the DIR() call for those files matching the pattern.
Earlier comments about searches for the same text string as was incorporated in the DIR() pattern search seems still apropos, however.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Desktop en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by