How can I load my .mat files from a folder into a function?

This is my code and it calls "my_function", first file from the folder runs through the code successfully and then, error shows up.
% fetch the mat files
dirPath = '/path';
files = dir(fullfile(dirPath, '*.mat'));
% loop over the files and put them through my function
for i = 1:numFiles
fileName = files(i).name;
data = load(fullfile(dirPath, fileName));
my_function(data);
end
This is my_function which is called above.
function my_function(matfile)
%line 35
load(matfile);
This error show up when I run my code above.
Error using load
Argument must be a text scalar.
Error in my_function (line 35)
load(matfile);

Answers (1)

The ‘data’ variable contains a structure (see struct for details) holding all the variables in the file. See the documentation section on Load List of Variables into Structure Array for details
The structure fields need to be addresed to use the data within them. Other options to do that would be struct2table or struct2cell.

Asked:

on 22 Apr 2023

Answered:

on 22 Apr 2023

Community Treasure Hunt

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

Start Hunting!