How can I load my .mat files from a folder into a function?
2 次查看(过去 30 天)
显示 更早的评论
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);
0 个评论
回答(1 个)
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.
0 个评论
另请参阅
类别
在 Help Center 和 File 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!