How to efficiently load a large Matrix into a Matlab Function Block in Simulink
显示 更早的评论
Hello everyone,
I want to upload a large matrix A into a (embedded) Matlab Function Block in Simulink ver. 2021b. However, all the methods I have found or came up with seem to be very inefficient, leading to very large simulation times.
The matrix itself does not change over the course of the simulation, ideally I would import it once and reuse it in every time step.
My first Idea was to simply load the matrix in to the workspace and declare it as a Parameter in the Function Block, i.e.
function func = fcn(A,inputs)
%%% Code %%%
end
however, this leads to the same problem that was described in an old question, which went unanswered back then: The compilation of the model takes upwards of 15 minutes, sldiagnostics shows that the step "Stateflow post-compile notify" alone takes about 900 seconds at times.
This post suggests that disbaling data validation and/or debugging for the Matlab Function Block helps, but I was unable to find how this works in the current version.
My second approach was to load the data inside the Block, like this:
function func = fcn(inputs)
coder.extrinsic('load');
S = load('A.mat') % because simulink warned me to do it like this
A1 = S.A;
end
however, this does not work because of the error
Attempt to extract field 'A' from 'mxArray'.
which I was able to solve by instead using
function func = fcn(inputs)
coder.extrinsic('load');
coder.extrinsic('getfield');
S = load('A.mat') % because simulink warned me to do it like this
A = zeros(N); % preinitialize, N being the size of A
A1 = getfield(S,'A');
end
Which compiles quickly, but the simulation itself takes a very long time, I assume because the data is imported in every timestep.
Does anybody have an idea how to do this more efficiently?
Best regards
Folke
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Sources 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!