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

2 visualizaciones (últimos 30 días)
Himangshu
Himangshu el 22 de Abr. de 2023
Respondida: Star Strider el 22 de Abr. de 2023
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);

Respuestas (1)

Star Strider
Star Strider el 22 de Abr. de 2023
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.

Categorías

Más información sobre Workspace Variables and MAT Files 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!

Translated by