How do I access field name elements from structure?

50 visualizaciones (últimos 30 días)
Kristan Sears
Kristan Sears el 18 de Sept. de 2025 a las 20:24
Editada: dpb el 20 de Sept. de 2025 a las 13:25
I generated a list of file names using the follwoing:
m = dir([source,'/*.MAT'])
This provided the desired results, but returns a structure with fields. I want to access the elements of m.name, so that I can loop through each file and perform actions.
If I run;
m.name
ans =
'CFGC1-1000_1.MAT'
ans =
'CFGC1-750_1.MAT'
ans =
'CFGC1-800_1.MAT'
ans =
'CFGC1-850_1.MAT'
ans =
'CFGC1-900_1.MAT'
ans =
'CFGC1-950_1.MAT'
How do I access m.name so that I can index each field value separately?

Respuesta aceptada

dpb
dpb el 18 de Sept. de 2025 a las 20:34
m = dir([source,'/*.MAT']);
for i=1:numel(m)
fn=fullfile(m(i).folder,m(i).name);
...
end
Congratulations on the use of dir() here with the wildcard; it is one of the most efficient ways to do such...
  9 comentarios
Kristan Sears
Kristan Sears el 19 de Sept. de 2025 a las 0:40
This is fantastic! Thank you so much for all of the help. This is not long-winded at all. If it's not obvious, I'm a novice user to Matlab and I really appreciate the help.
Thank you for explaining. That helps a lot. And I appreciate the time you took to explain this. I need to practice with what you have shown me. I didn't realize I was making the mistakes you pointed out. The fullfile method makes a lot more sense than the way I was doing it.
dpb
dpb el 19 de Sept. de 2025 a las 14:38
Editada: dpb el 20 de Sept. de 2025 a las 13:25
No problem, glad to help. Trying to teach some as well as just straight answer is part of my DNA... <g>
"The fullfile method makes a lot more sense than the way I was doing it."
Yes, besides simply the easier syntax of the function call, fullfile uses the OS-specific file separator character so your m-code is then more able to be used elsewhere if that ever came up in working with colleagues. It's just better practice to get into the habit early rather than trying to break bad habits later...

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Workspace Variables and MAT Files en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2025a

Community Treasure Hunt

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

Start Hunting!

Translated by