How to return number of directory to label objects in a loop

5 Ansichten (letzte 30 Tage)
Sarah Bowling
Sarah Bowling am 24 Nov. 2020
Bearbeitet: TARUN am 26 Apr. 2025
I would like to run a loop where I open a folder, load a matlab file, and label it with a number that increases with each folder I open (ie matlab file from first folder is 1, from second folder is 2 ect). I am using the following command to open each folder and load the matlab file. How do I return the column number from D so that I can label the object with the no. of directory I've opened?
D = dir('Index*')
for k = 1:length(D)
currD = D(k).name
cd(currD)
load 'Summary.mat' summary
# Now I want to label the number of the directory I've opened as = summary
cd ..

Antworten (1)

TARUN
TARUN am 26 Apr. 2025
Bearbeitet: TARUN am 26 Apr. 2025
If you want to label each loaded summary variable with the number of the directory you've opened, you can simply add a new field or variable to your summary struct or variable after you load it.
Here’s how you can do it:
D = dir('Index*');
for k = 1:length(D)
currD = D(k).name;
cd(currD)
load('Summary.mat', 'summary')
summary.dir_number = k; % Add a field with the directory number
% Now you can save or process summary as needed
cd ..
end
With this modification, summary.dir_number will contain the index of the directory.

Kategorien

Mehr zu File Operations finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by