GUI selecting multiple files with same amount of digits in filename
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
0 votes
1 Comment
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.
Accepted Answer
20 Comments
This works great. If I want to indicate that the cell array is 0x0, how can MATLAB provide this as an error? I have tried:
if nnz = 1
set(handles.text1, 'String', C{X})
else
Errorfilename = msgbox('No files found with input criteria', 'Error', 'error')
end
But it says that it does not have enough input arguments
>> which nnz built-in (C:\ML_R2017\toolbox\matlab\sparfun\@double\nnz) % double method >> help nnz nnz Number of nonzero matrix elements. nz = nnz(S) is the number of nonzero elements in S.
@Debbie Oomen. I suspect that you want the function isempty, rather than the function nnz.
I am trying that now. This is the code I have tried:
foldername = handles.foldername; %see ChooseFolder callback Pattern = handles.Pattern; %see prefix callback
filepattern = fullfile(foldername, Pattern); %all files in folder with ID*sec.csv* pattern
filesfolder = dir(filepattern); %files with the pattern
filenames = struct2cell(filesfolder);
filenames = filenames(1,1:end)'; %filenames
Filestringcheck = strfind(filenames,'csv'); %# of strings in filename without prefix
Filestringlogic = cellfun(@isempty,Filestringcheck);
filesfolder = filesfolder(~[filesfolder.isdir]); %remove folders
NoD= cellfun(@isempty, filenames,'digit'); %count number of digits in filename
Dig = str2double(get(handles.digitsfile, 'String')); %gets desired number of digits from user
Matches = NoD == Dig;
Filenames{Matches}
if isempty(Filestringlogic) == 1;
ErrorFileName = msgbox('No files found with input criteria', 'Error','error');
end
if is empty ('digit')
This gives me an error. I have no idea where to put the @isempty
Matches is a logical vector, where its true values correspond to matched filenames that you want... if you want to check if there were no matches, then you can do this trivially like this:
if ~any(Matches)
PS: Note that for some reason you deleted the isstrprop function... check my answer again, because this will not work and it is not what I showed you in my answer:
NoD= cellfun(@isempty, filenames,'digit');
PPS: This code:
Filenames{Matches}
simply displays the contents of the cell array Filenames for the cells select by Matches, it does nothing else at all. But if you want to put those selected names in another variable, then you will need to allocate them to that variable:
Z = Filenames(Matches);
PPPS: I really would recommend that you put the file extension pattern into the dir pattern. I.e. instead of this (and associated code):
Filestringcheck = strfind(filenames,'csv');
use a suitable dir pattern. There is nothing that strfind can do that dir cannot do, so there is no point in making your code more complex by replicating functionality in two places.
More Answers (0)
Categories
Find more on Environment and Settings in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)