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

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

C is an empty cell with spaces available for each file? Or how should I interpret the first line?
That's preallocation.
okey I appreciate the response but it did not solve my problem.
Stephen23
Stephen23 2021년 2월 25일
편집: Stephen23 2021년 2월 25일
@madhan ravi: perhaps the function output should be changed to C.
Thank you Stephen.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2021년 2월 25일

댓글:

2021년 2월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by