Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB dir without '.' and '..'

Tags:

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!

Edit:

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.

like image 563
jeff Avatar asked Dec 06 '14 22:12

jeff


People also ask

What does dir () do in MATLAB?

dir lists files and folders in the current folder. dir name lists files and folders that match name .

How do I get the current directory in MATLAB?

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.


1 Answers

A loop-less solution:

d=dir; d=d(~ismember({d.name},{'.','..'})); 
like image 53
Trisoloriansunscreen Avatar answered Sep 28 '22 12:09

Trisoloriansunscreen