read multiple mat files with Simulink

5 次查看(过去 30 天)
SS
SS 2022-8-8
回答: Suman 2024-7-26
I want to load multiple data with Simulink.
I know there is a from file block in Simulink.
But from file block can read only one data.
I want to read multiple data,so I think to use MATLAB Function block.
Please tell me a solution.

回答(1 个)

Suman
Suman 2024-7-26
Yes since you want to load multiple .mat files, it is best to use a MATLAB function block.
Here is a simple example code:
function data = readMatFiles()
matFiles = {'file1.mat', 'file2.mat', 'file3.mat'};
%an empty array or structure to hold the data
data = [];
for i = 1:length(matFiles)
fileData = load(matFiles{i});
if isempty(data)
data = fileData;
else
data = [data; fileData];
end
end
end
You should be careful about few points when using MATLAB Function block to load data:
  • Data Format: Ensure that the data format in the .mat files is consistent and compatible with how you intend to use it in Simulink.
  • File Paths: If your .mat files are not in the current working directory, provide the full path to each file.
  • Performance: Loading multiple .mat files can be time-consuming if the data is very large. You might consider loading the data into model workspace at model load time and then using the data as required.

类别

Help CenterFile Exchange 中查找有关 Modeling 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by