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

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

采纳的回答

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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by