MatLab list files (excluding directories) in the folder

So, I know that I can list all the files and directories in my current folder using functions such as dir()or ls(), and I know that it was once specified, I can tell them about each other with the field isdir.

But is there a way to exclude directories from the very beginning and list only the files?

Even better, is there a way to exclude current directories .and parent directories ..- which will (of course) show each time - and list every other file and directory? Seriously, who uses ls(), wondering if there is one .?

+3
source share
2 answers

dir - , . , , . :

list=dir();
CleanList=setdiff({list.name},{'.','..'})'; 
+2

, , , ?

:

function list = files_dir(varargin)
% Similar functionality to 'dir', but only returns files (no folders)

list = dir(varargin{:});

list([list.isdir]) = [];

, .

, '.' '..':

function list = dir_exclude_self(varargin)
% same as 'dir', but doesn't return '.' or '..'

list = dir(varargin{:});

self_indices = ismember({list.name}, {'.', '..'});
list(self_indices) = [];

, , , MATLAB startup.m.

+1

All Articles