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

조회 수: 8 (최근 30일)
Himangshu
Himangshu 2023년 4월 22일
답변: Star Strider 2023년 4월 22일
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);

답변 (1개)

Star Strider
Star Strider 2023년 4월 22일
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.

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by