Input signal as vector with time in simulink

I'm working with an L2 S-function, and I have three input ports. My S-function is already working fine, so I can enter my three inputs, and my simulation will work properly. Now, I want to compare my simulation with a real system. I conducted measurements on a system and obtained a vector with the same length over the same time for each input. The issue is that Simulink/my S-function does not accept vectors as inputs, and Simulink operates based on the time provided within Simulink.
Let's assume we have something similar to the picture/table below: the first column is time, the second column is the first input, etc.
I want Simulink to precisely follow the timeline from my measurement data. At each specific time point, I aim for Simulink to extract the corresponding value and traverse through the S-function until reaching the end. Is it possible to achieve this?
Thank you for any suggestions
I attempted to manually run the simulation using MATLAB with two 'for-loops,' and it worked correctly. Now, it would be fantastic to achieve the same functionality within Simulink
I tried to pass the values with "From Workspace" like: u1 = [Data(:,1) Data(:,2)], u2 = [Data(:,1) Data(:,3)] and u3 = [Data(:,1) Data(:,4)] but the result ist just not plausible

 Accepted Answer

I solved my problem. It was not because I inported the data the false way, but because of an error on how to handelt my s-function while using Simulink. I tried another way to solve the differential equation within the s-function and it worked properly. So there must be an error in the first way.
Thanks for your answers because I could say for sure that I'm importing the data the right way so I was able to look for the error somewhere else

2 Comments

Out of curiosity, why use an s-function to solve a differential equation instead of using the Simulink solver?
TKinter
TKinter on 4 Dec 2023
Edited: TKinter on 4 Dec 2023
It is not just about solving "a" differential equation but the problem I wanted/want to solve is really complex an requires a lot of connexion which is almost not possible to do in simulink (possible but there would be no way to see through for myself or if you have to explain to others). So the workaround is to use level2 s-function. That is also the reason why I haven't tired to explain/show the s-function here because of the complexity of the problem.

Sign in to comment.

More Answers (2)

I don't understand why your S-function block needs to get three, 2d arrays (e.g. u1 =[(Data(:,1),Data(:,2)], with time as the first column) as inputs, as you imply in your description of trying to supply inputs using a from-workspace block.
If your function has three input signals, it should just receive a scalar value for each input at each Simulation time step (just like every other Simulink block). The from workspace block, is designed to feed these scalar values, at each simulation step. It looks up the time, and, interpolates (according to the interpolation method that is set) the current value to be output and fed to your system.
If my explanation does not allow you to fix your problem and move ahead, please explain further, and attach the code for your level 2 S function block

7 Comments

Hi first thanks for your answer.
well the problem is, From Workspace does not accept a vector with one column.
Here the error from simulink when i tried to just feed the From Workspace with only the signal values as vector:
The matrix data must be a real, double, have two dimensions, and at least two columns. The first column must contain time values and the remaining columns the data values. Matrix values cannot be Inf or NaN.
That is why I also gave the Time as first column and the values as second column
Jon
Jon on 30 Nov 2023
Edited: Jon on 30 Nov 2023
You should just assign one numPoints by four array as the simin argument to the from workspace block, where the first column of this array is the time vector. If needed separate the output of the from Workspace block into three separate signals to feed to your s-function block using a demux.
I have attached an example
Did this help?
Hi Jon,
thanks for your suggestion. I'll try it tomorroy when I'm back in my city and I will let you know
can you save please save your file for version R2023a? because I can't open it with my Matlab Version (R2023a)
I guess you don't need the R2023a version anymore, as it seems like you've answered your own question, but let me know.
P.S. Why not update to the current version, usually helpful to keep current
Yes I don't need it anymore thanks.
I will update it

Sign in to comment.

Fangjun Jiang
Fangjun Jiang on 30 Nov 2023
Edited: Fangjun Jiang on 30 Nov 2023
u1 = [Data(:,1) Data(:,2)], u2 = [Data(:,1) Data(:,3)] and u3 = [Data(:,1) Data(:,4)]
The way you construct the data for "From Workspace" block to import measured inputs is correct.
You could use just one "From Workspace" block and specify the data as "Data". It will import three signals at once and then you can use a "Demux" block to split it into three and feed them to your S-function.
"I want Simulink to precisely follow the timeline from my measurement data."
If you mean to set the simulation time vector exactly as your first column data, that is possible but not a good idea. It looks like the measurement is sampled at 1 second with offset of 0.4. But there are missing data at 7.4 and 9.4 for example.
If you mean to set the three input data for the simulation "exactly" the same as the measured data, you have done the correct thing and that is sufficient.
Maybe, your simulation output "result is just not plausible" comparing to your real, measured output, that is most likely caused by something else, not the constructing of the input data. A few things to look out is the solver and simulation step size, the sample time setting for the "From Workspace" block and its interpolation methods. Add a scope to observe the output of the "From Workspace" block during the whole simulation to see if the value makes sense.
The first measured data is at t=0.4 while the simulation usually starts at t=0. There might be an initial value problem. Add a row to specify the data for t=0 or you could set the simulation start time to be 0.4

3 Comments

Thanks for your answer.
when i look in the simulation time from the output, it did start from 0 and it interpolet the values between times. so I thinks it is good so far.
"If you mean to set the three input data for the simulation "exactly" the same as the measured data, you have done the correct thing and that is sufficient". And how can I let simulink decide itself for the simulation time?
it wrote the simultion with matlab and the results were as expected since I knew exactly what I was doing. and in Simulink it can't be always controlled what is going on in the block. That is why I was/am thinking that and not passing the data the right way
If you observed the outputs of the three "From Workspace" blocks and they are as expected, then there is no problem regarding data importing. The unexpected simulation output was caused by something else, which you have not provided any clues.
Yeah I first thought that the problem ist how I'm impoting the data but maybe there is a problem on my s-function. So If everybody here is saying that I've imported the data the right way then I will check on my s-function itself again. Thanks

Sign in to comment.

Categories

Find more on Simulink Coder in Help Center and File Exchange

Asked:

on 30 Nov 2023

Commented:

on 5 Dec 2023

Community Treasure Hunt

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

Start Hunting!