How do I remove all filenames from my cellarray with .m ending, if else how do I save those with .bmp?

1 回表示 (過去 30 日間)
I have saved all filenames in a cell array for a given folder however, I only want it to include the files with ending .bmp.
function filer = filnamn(katalog)
%Ger en cell med de filer som finns i sökta katalog.
filer = dir(katalog);
filer = {filer(~[filer.isdir]).name};
end
How would I go about doing this?
  2 件のコメント
Stephen23
Stephen23 2021 年 2 月 26 日
"How do I remove all filenames from my cellarray with .m ending, if else how do I save those with .bmp?"
Note that in general code files and data files should be stored separately. Keeping data files off the Search Path will improve MATLAB efficiency when changing directories or adding files.

サインインしてコメントする。

採用された回答

KSSV
KSSV 2021 年 2 月 26 日
idx = contains(filenames,'.bmp') ;
filenames(idx)

その他の回答 (1 件)

Stephen23
Stephen23 2021 年 2 月 26 日
編集済み: Stephen23 2021 年 2 月 26 日
Change the DIR call to specify the file extension:
filer = dir(fullfile(katalog,'*.bmp'));
This is more efficient than getting the entire file content and then removing some names (e.g using contains).

カテゴリ

Help Center および File ExchangeWorkspace Variables and MAT Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by