Input signal as vector with time in simulink
Show older comments
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
More Answers (2)
Jon
on 30 Nov 2023
0 votes
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
TKinter
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
Jon
on 1 Dec 2023
Did this help?
TKinter
on 1 Dec 2023
TKinter
on 2 Dec 2023
Jon
on 4 Dec 2023
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
TKinter
on 5 Dec 2023
Fangjun Jiang
on 30 Nov 2023
Edited: Fangjun Jiang
on 30 Nov 2023
0 votes
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
TKinter
on 1 Dec 2023
Fangjun Jiang
on 2 Dec 2023
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.
TKinter
on 3 Dec 2023
Categories
Find more on Simulink Coder in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!