Error while using Simulink Programatic Interface for multiple inputs
Afficher commentaires plus anciens
While implementating the Programatic Interface I came across the issue of an error being thrown when the input size is larger than one.
For a one dimensional input, with the simulink model seen below. The file is saved as scratch.

[~, ~, ~, ~] = scratch([], [], [], 'sizes');
[sys, x0, blks, st] = scratch([], [], [], 'compile');
u = zeros(1, 1);
out = scratch([], u, 0.1, 'outputs')
This results in an output of:
out = 0
However when implementing two input ports seen below, an error is thrown.

[~, ~, ~, ~] = scratch([], [], [], 'sizes');
[sys, x0, blks, st] = scratch([], [], [], 'compile');
u = zeros(2, 1);
out = scratch([], u, 0.1, 'outputs')
The error states:
"Initial state vector "X0" must be a real vector of length 1"
What could the issue be?
Réponse acceptée
Plus de réponses (1)
Raghvi
le 13 Mar 2023
Hey Fredrik,
To compute the output, the following line of code is used:
out = modelName(x,u,t,'outputs');
Where "x" are the state values, specified as a vector of real double values or as a structure.
You are using the command:
out = scratch([], u, 0.1, 'outputs')
Putting "x" as an empty vector. This is what seems to be causing the trouble. The initial state should not be empty. You might have received a warning while doing the same with a single input as well. If you explicitly define your initial state, the error goes away. Additionally, the time "t" must be real, double and scalar.
For additional information, you can refer to the following documentation:
3 commentaires
Fredrik Rhenman
le 13 Mar 2023
Modifié(e) : Fredrik Rhenman
le 13 Mar 2023
Raghvi
le 13 Mar 2023
Can you let me know what block parameters are you using so I can replicate the error at my end.
Raghvi
le 13 Mar 2023
I have found that since there are two inputs, you need to provide two initial states, for for each of them. Giving a 1x2 array as "x" resolved the issue for me.
Catégories
En savoir plus sur Simulink dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!