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

1 Ansicht (letzte 30 Tage)
Sander Janssen
Sander Janssen am 11 Okt. 2022
Kommentiert: Sander Janssen am 11 Okt. 2022
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!

Akzeptierte Antwort

Davide Masiello
Davide Masiello am 11 Okt. 2022
Bearbeitet: Davide Masiello am 11 Okt. 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

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by