How can I save values to an array using a for loop

1 view (last 30 days)
I am trying to create an array or variable that stores filenames for later access. I have tried doing this with a for loop but am forgetting something or doing it wrong. My goal is something similar to:
folderName = [dir([read_dr '\some_folder'])]; %This works
for g = 1:length(folderName)
fileNames = folderName(g).name;
end
disp(fileNames) % Will only show the last value
This will store the value but will only save the last one.
I have also tried:
folderName = [dir([read_dr '\some_folder'])]; %This works
fileNames = {};
for g = 1:length(folderName)
fileNames(g) = folderName(g).name;
end
Which will give the error:
Conversion to cell from char is not possible.
Any help is greatly appreciated!

Accepted Answer

Davide Masiello
Davide Masiello on 11 Oct 2022
Edited: Davide Masiello on 11 Oct 2022
I'd try this
folderName = [dir([read_dr '\some_folder'])]; %This works
fileNames = {};
for g = 1:length(folderName)
fileNames{g} = folderName(g).name;
end

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!