Variable sample time with different time values

1 次查看(过去 30 天)
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
  4 个评论
Mathieu NOE
Mathieu NOE 2021-1-12
hello
my first idea would be to make the simulink (fixed) steps = 0.02 (because then you can "pick" the data with time index whatever time value as soon as they are multiples of 0.02 , which is the case for the time values you are looking for)
simulink time simulation must be set from 0 to 2 s.
of course the array generated by simulink will have more values than needed , but you simply have to add in your script that you want only those values : see example below (tested with dummy data - to be replaced by your simulink array)
time = 0:0.02:2; % dummy data
data = (1:length(time))*5; % dummy data
tt = [0,0.7,1.14,2]; % time values for data retrieval
idx = ismembertol(time(:), tt(:));
c = 1:length(time);
d = c(idx) % searched time indexes
extracted_data = data(d)
Philipp Vogel
Philipp Vogel 2021-1-14
Hello Mathieu,
thank you for your model proposal.
Regards,
Philipp

请先登录,再进行评论。

采纳的回答

Fangjun Jiang
Fangjun Jiang 2021-1-12
编辑:Fangjun Jiang 2021-1-12
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Sources 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by