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

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

回答(1 个)

TARUN
TARUN 2025-4-26
编辑:TARUN 2025-4-26
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.

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by