Display file names from current directory if more than n characters

1 回表示 (過去 30 日間)
Dobs
Dobs 2021 年 11 月 30 日
コメント済み: Dobs 2021 年 11 月 30 日
Hi,
I want to write code that returns the filenames in my current directory, but only those filenames with more than n characters.
My code so far:
directory = dir('*.*'); % specifiy files, leave out folders
% remove the two '.' and '..' folders at the top
if length(directory) >= 3
directory = directory(3:end);
end
But how do I select only those filenames with more than n characters? I thought about setting up a logical vector, but I don't know how to do that. Does anybody have any suggestions?
Many thanks,
Dobs
  2 件のコメント
Stephen23
Stephen23 2021 年 11 月 30 日
編集済み: Stephen23 2021 年 11 月 30 日
"But how do I select only those filenames with more than n characters?"
Is that actually the goal, or is this really just your attempt to remove the dot directory names?:
Note that the dot directories are not guaranteed to be the first and second names returned, so your code already has a bug in it:
Dobs
Dobs 2021 年 11 月 30 日
編集済み: Dobs 2021 年 11 月 30 日
Yes, selecting the filenames with more than n characters is actually the goal because it's for an assignment.

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

採用された回答

Stephen23
Stephen23 2021 年 11 月 30 日
編集済み: Stephen23 2021 年 11 月 30 日
n = 5;
P = 'absolute or relative path to where the files are saved';
S = dir(fullfile(P,'*.*'));
C = {S(~[S.isdir]).name}; % comma-separated lists
X = cellfun(@numel,C)>n;
C = C(X)

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by