Clear Filters
Clear Filters

read multiple mat files with Simulink

6 views (last 30 days)
SS
SS on 8 Aug 2022
Answered: Suman on 26 Jul 2024 at 11:37
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.

Answers (1)

Suman
Suman on 26 Jul 2024 at 11:37
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.

Categories

Find more on Modeling in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!