the function dir
returns an array like
. .. Folder1 Folder2
and every time I have to get rid of the first 2 items, with methods like :
for i=1:numel(folders) foldername = folders(i).name; if foldername(1) == '.' % do nothing continue; end do_something(foldername) end
and with nested loops it can result in a lot of repeated code.
So can I avoid these "folders" by an easier way?
Thanks for any help!
Lately I have been dealing with this issue more simply, like this :
for i=3:numel(folders) do_something(folders(i).name) end
simply disregarding the first two items.
BUT, pay attention to @Jubobs' answer. Be careful for folder names that start with a nasty character that have a smaller ASCII value than .
. Then the second method will fail. Also, if it starts with a .
, then the first method will fail :)
So either make sure you have nice folder names and use one of my simple solutions, or use @Jubobs' solution to make sure.
dir lists files and folders in the current folder. dir name lists files and folders that match name .
Open the Current Folder Browser MATLAB Toolstrip: On the Home tab, in the Environment section, click Layout. Then, in the Show section, select Current Folder.
A loop-less solution:
d=dir; d=d(~ismember({d.name},{'.','..'}));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With