How to return number of directory to label objects in a loop
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 ..
0 comentarios
Respuestas (1)
TARUN
el 26 de Abr. de 2025
Editada: TARUN
el 26 de Abr. de 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.
0 comentarios
Ver también
Categorías
Más información sobre File Operations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!