How do I save filenames in a for loop for later access?

1 回表示 (過去 30 日間)
Geeniee
Geeniee 2021 年 2 月 25 日
コメント済み: Geeniee 2021 年 2 月 25 日
I'm able to iterate through a given folder and display the names of all files. However, I want to save the names so did I can do work on each file. For example,
function fn = filnamn(katalog)
% displays the names of all files in given diretory
% Called with name of desired directory
fn = dir(katalog);
for k = 1:numel(fn)
if ~fn(k).isdir
disp(fn(k).name);
end
end
After each iteration how would I "append" each file so did I can access them afterwards?
  2 件のコメント
Geeniee
Geeniee 2021 年 2 月 25 日
Thank you that is what I was looking for!

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

採用された回答

madhan ravi
madhan ravi 2021 年 2 月 25 日
編集済み: madhan ravi 2021 年 2 月 25 日
C = cell(nnz(~fn(k).isdir), 1); % before loop
function C = filnamn(katalog)
% displays the names of all files in given diretory
% Called with name of desired directory
fn = dir(katalog);
for k = 1:numel(fn)
if ~fn(k).isdir
C{k} = fn(k).name;
end
end
  5 件のコメント
Stephen23
Stephen23 2021 年 2 月 25 日
編集済み: Stephen23 2021 年 2 月 25 日
@madhan ravi: perhaps the function output should be changed to C.
madhan ravi
madhan ravi 2021 年 2 月 25 日
Thank you Stephen.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSearch Path についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by