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

3 views (last 30 days)
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 ..

Answers (1)

TARUN
TARUN on 26 Apr 2025
Edited: TARUN on 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.

Categories

Find more on File Operations in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!