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

조회 수: 5 (최근 30일)
Sarah Bowling
Sarah Bowling 2020년 11월 24일
편집: TARUN 2025년 4월 26일
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