Error while using Simulink Programatic Interface for multiple inputs

3 views (last 30 days)
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?

Accepted Answer

Raghvi
Raghvi on 15 Mar 2023
It has come to my attention that the issue you are experiencing may be related to a problem with the documentation.
Instead of:
out = modelName(x,u,t,'outputs');
It should be
out = modelName(t, x, u,'outputs');
I have tried it with this command and it works.
The corresponding team is actively working towards resolving the issue and I appreciate your patience and understanding.

More Answers (1)

Raghvi
Raghvi on 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 Comments
Raghvi
Raghvi on 13 Mar 2023
Can you let me know what block parameters are you using so I can replicate the error at my end.
Raghvi
Raghvi on 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.

Sign in to comment.

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!