MATLAB: Variable sample time with different time values

sample timesimulink

Hello,
i have the following data,for example and a simulation duration in simulink for 2 seconds.
Time Parameter
0 4
0.7 8
1.14 10
2 7
With a From Workspace Block i transfer the time and parameter data to simulink. With a To Workspace Block i transfer the results to matlab workspace.
Which setting do i have to change in simulink or how do i set the sample time in the To Workspace Block
that i got four results for 0,0.7,1.14 and 2 seconds to the matlab workspace?
Regards,
Philipp

Best Answer

This seemingly simple question is actually quite complex.
The input data is non-periodic. To hit every value-changing data point, the simulation step size needs to be the largest common factor of 0.7, 1.14 and 2, which is 0.02. So set the simulation to be fixed step size, discrete solver and step size to be 0.02.
To capture every value-changing data point and nothing more, it needs to use a "Detect Change" block to detect the input change and then enable the "To Workspace" data capture only at that moment.
The "interpolate data" option of the "From Workspace" block needs to be un-checked. The "Form output after final data value by" needs to be set as "holding final value".
Attached is the example model in R2019b.
>> out.simout.Time
ans =
0
0.7000
1.1400
2.0000
>> out.simout.Data
ans =
4
8
10
7