How do I access field name elements from structure?

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?

 Accepted Answer

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 Comments

ADDENDUM:
See <Stucture Arrays> for the complete treatise on accessing structure arrays...there's a lot to be learned there including more advanced "tricks" like variables as field names, etc., ... that solve a myriad of otherwise difficult problems.
Ah, okay, now I understand. I saw that notation in the help documentation, but I didn't fully understand it until I saw your reply. Thanks for hte help. Much appreciated.
Brilliant. That works exaclty like what I wanted. I like the power that structures offer. I'm just new to using them and accessing the elements.
Yes, you first access the element of the array and then dereference the fieldname with the dot.
You can get clever with for and the array form of for index = values...
For an example set of .mat files locally,
>> d=dir('*.mat'); % get the dir() struc
>> for fn=fullfile({d.folder},{d.name})
disp(fn)
end
{'C:\Users\Duane\Documents\MATLAB\Utilities\StateAbbreviationsTable.mat'}
{'C:\Users\Duane\Documents\MATLAB\Utilities\ZipMap.mat'}
{'C:\Users\Duane\Documents\MATLAB\Utilities\zipDatabaseTable.mat'}
>>
MATLAB takes each column in the values array and passes it to the loop index in turn...rather than using the counted loop.
Here, the cell array input form of fullfile to build the array of fully-qualified filenames first...it saves the individual call in the counted loop at the expense of the extra memory to hold the combined names temporary array.
I'd also emphasize the use of the fully-qualified filenames instead of the often-seen use of cd to change the working directory.
I may be going about this the wrong way. I was messing around in the command window and I got this to work:
path = strcat(source,'/', string(m(1).name))
load(path)
I have to get the path inputs to be strings. I'm not sure how to do that by referencing the fields in the structure. I trying to used the source path that I created using:
source = uigetdir([]);
m = dir([source,'/*.MAT']);
for fn = [{m.name}]
disp(fn)
end
I can display all the file names, but I'm struggling to figure out how to reference them one at a time for a load step.
My objective is to load each .MAT file using the file names and path from above. I would like to rename each loaded .MAT file during the load step.
I managed to concatenate the path and the file name from the fields to get this:
strcat(source,'/',fn(:))
ans =
6×1 cell array
{'/MATLAB Drive/PSD/CFGC1/CFGC1-1000_1.MAT'}
{'/MATLAB Drive/PSD/CFGC1/CFGC1-750_1.MAT' }
{'/MATLAB Drive/PSD/CFGC1/CFGC1-800_1.MAT' }
{'/MATLAB Drive/PSD/CFGC1/CFGC1-850_1.MAT' }
{'/MATLAB Drive/PSD/CFGC1/CFGC1-900_1.MAT' }
{'/MATLAB Drive/PSD/CFGC1/CFGC1-950_1.MAT' }
load(fn(1))
Error using load
Argument must be a text scalar.
But now I'm at a loss how to extract the full path and file name for the load statement.
First of all don't alias the built in MATLAB path function; this will wreak havoc and be exceedingly difficult to poinpoint what went wrong when it does unexpected things.
Subsequently
path = strcat(string(m(2).folder),'/', string(m(1).name))
is identically the same as the far better construct
fn=fullfile(m(i).folder,m(i).name);
which will build the fully-qualified file name using consistent folder and name parts (that may have just been a typo in the post, but mixing m(2) and m(1) is asking for trouble as well -- if the dir search was over only one folder, it won't show up because the folder will be the same for all files, but if you do the directory search over multiple directories/folders, then that won't be true and you'll be creating filenames that don't match.
You don't HAVE to have the fullfy-qualified filename be a string; all the MATLAB file i/o functions accept the char string array you get by default from the above use of fullfile so the following pattern works -- illustrated again from the local .mat files.
>> i=1; % fake loop index
Invalid use of operator.
>> fn=fullfile(d(i).folder,d(i).name) % fully-qualified filename
fn =
'C:\Users\Duane\Documents\MATLAB\Utilities\StateAbbreviationsTable.mat'
>> load(fn) % load that file
>> whos('-file',fn) % show what is in it
Name Size Bytes Class Attributes
tAbbrevState - 14829 table
>> head(tAbbrevState) % show we loaded that table variable
ans =
8×2 table
Abbr State
______ _______________
{'AL'} {'Alabama' }
{'AK'} {'Alaska' }
{'AZ'} {'Arizona' }
{'AR'} {'Arkansas' }
{'CA'} {'California' }
{'CO'} {'Colorado' }
{'CT'} {'Connecticut'}
{'DE'} {'Delaware' }
>>
Unless you know the particular variable(s) in the .mat file as here and have code (as I do) written to use those specific variables, the more generic way to write code is to use the return value style of load in which a struct is returned and the variables are fields within that struct.
If there really were some mandatory reason to use a string variable, the cleaner syntax would be
fn=string(fullfile(d(i).folder,d(i).name)); % cast fully-qualified filename to string
You didn't show why, specifically, you thought you need a string variable; perhaps my second example confused you in which one gets a cellstr array instead since can't catenate different-length char arrays.
In that case, simply dereference the cellstr with the curlies as
load(fn{i}) % dereference a cellstr to the underlying char string array.
Example--
fn={'C:\Users\Duane\Documents\MATLAB\Utilities\StateAbbreviationsTable.mat'} % cellstr
fn{:} % underlying char string
PS -- Ignore the error message above; that's just from trying to execute the code I pasted in from local machine with the prompt characters in front...
I was doing long-winded answer/comment while you were editing...
load(fn(1))
Error using load
Argument must be a text scalar.
Yes, when you catenated all together, you created a cellstr array.
source = uigetdir([]);
m = dir([source,'/*.MAT']);
With the above struct, you have two choices,
  1. Use loop as first shown...
for i=1:numel(m)
fn=fullfile(m(i).folder,m(i).name); % is char array
load(fn)
end
2. If do catenate first, dereference the cellstr array
for fn=filenames % the array of cellstr fully-qualified names
load(fn{:}) % dereference each one in turn
end
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 on 19 Sep 2025
Edited: dpb on 20 Sep 2025
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...

Sign in to comment.

More Answers (0)

Products

Release

R2025a

Asked:

on 18 Sep 2025

Edited:

dpb
on 20 Sep 2025

Community Treasure Hunt

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

Start Hunting!