My understanding is that you are trying to simulate a control system using an ‘S-function’ in MATLAB and Simulink.
Your ‘S-function’ is currently written as a ‘Level-1 S-function’, and the error you're encountering is likely due to the ‘sys’ variable not being assigned correctly as a scalar or a vector of length 1 in the ‘mdlOutputs’ function. This is a common issue with ‘Level-1 S-functions’, which can be less intuitive to debug and manage.
The better approach would be to use ‘Level-2 S-function’ instead of ‘Level-1 S-Function’, because ‘Level-2 S-functions’ offer greater flexibility, ease of maintenance, and higher likelihood for receiving updates and support for new features in Simulink.
You can follow the below steps to implement the logic in ‘Level-2 S-function’:
- Transfer your existing logic from the ‘Level-1 S-function’ to the appropriate ‘Level-2’ methods, such as ‘setup’, ‘InitializeConditions’, ‘Outputs’, and ‘Derivatives’
- In the ‘setup‘ function, register the number of continuous states, inputs, and outputs
- Ensure that block.NumContStates = 1; is specified in the ‘setup’ function to indicate that there is one continuous state. This line is essential for defining the number of continuous states your ‘S-function’ uses, allowing Simulink to allocate the necessary resources and ensure smooth execution of the simulation. Without this specification, any attempt to initialize or access continuous state data will lead to errors
- Replace the old ‘S-function’ block in your Simulink model with the new ‘Level-2 S-function’ and ‘run’ the simulation
The error that you were facing earlier should be resolved after this modification.
You can refer to the following documentation links for more details:
- https://www.mathworks.com/help/simulink/sfg/maintaining-level-1-matlab-s-functions.html#bq3i98j
- https://www.mathworks.com/help/simulink/sfg/what-is-an-s-function.html
I hope this helps!