read multiple mat files with Simulink

조회 수: 2 (최근 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에서 Sources에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by