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

1 次查看(过去 30 天)
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?

采纳的回答

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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Search Path 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by