How to efficiently load a large Matrix into a Matlab Function Block in Simulink

8 次查看(过去 30 天)
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

采纳的回答

Aniket
Aniket 2024-9-18
I understand that you want to know a way to efficiently load a large matrix into MATLAB Function Block in Simulink. In order to do so, consider the following approaches:
1. Model Reference for Compilation Efficiency:
- If the Embedded MATLAB Function Block does not change between simulation runs, place it inside a Model Reference. This can significantly reduce compilation time because referenced models are only recompiled when a change is detected. This approach can help mitigate long compilation times.
- For more information, refer to the MathWorks documentation on ‘Model References’:
2. Loading Data Efficiently:
- Pre-load the Matrix in the Workspace: Load the matrix into the MATLAB base workspace before starting the simulation. Use a persistent variable to store the matrix within the MATLAB Function Block, ensuring it is only loaded once. Here's an example:
function y = fcn(u)
coder.extrinsic('evalin');
persistent A;
if isempty(A)
A = evalin('base', 'A'); % Load matrix from workspace
end
% Use matrix A in computations
y = A * u;
end
- Using `coder.extrinsic`: If loading the matrix within the block is necessary, use `coder.extrinsic` with a persistent variable to ensure the matrix is loaded only once:
function y = fcn(u)
coder.extrinsic('load');
persistent A;
if isempty(A)
S = load('A.mat');
A = S.A;
end
% Use matrix A in computations
y = A * u;
end
3. Disable Data Validity Checking:
Slow simulation speed might be due to data validity checking, which is enabled by default. Disabling this can improve performance. For more information, visit: https://www.mathworks.com/help/simulink/gui/parameterwriterblockvalidation.html
4. Avoid Unnecessary Data Copies:
To further optimize code and reduce computation time, avoid unnecessary copies of data. MATLAB's handling of variable references can be optimized by understanding how data is copied. For detailed guidance, see:
Implementing these strategies should efficiently manage the large matrix in the Simulink model, reducing both compilation and simulation times.
Hope it helps!

更多回答(0 个)

类别

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

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by