MATLAB Answers

Variable sample time with different time values

51 views (last 30 days)
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 Comments

Show 1 older comment
Philipp Vogel
Philipp Vogel on 11 Jan 2021
Hello Mathieu,
i have a time-dependent model in simulink. For my example, I want to have the results
calculated for 0,0.7,1.14 and 2 seconds.
In the To Workspace Block the save format is array.
My problem ist the sample time in the To Workspace Block.
If I set a sample time of 0.4 seconds in the To Workspace block, then I get a solution vector in
the matlab workspace with 6 values for 0,0.4,0.8,1.2,1.6 and 2 seconds.
But I want a solution vector with 4 values for 0,0.7,1.14 and 2 seconds.
Which setting do i have to change in simulink or how do i set the sample time in the To Workspace Block to achieve this?
Regards,
Philipp
Mathieu NOE
Mathieu NOE on 12 Jan 2021
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 on 14 Jan 2021
Hello Mathieu,
thank you for your model proposal.
Regards,
Philipp

Sign in to comment.

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 12 Jan 2021
Edited: Fangjun Jiang on 12 Jan 2021
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

  1 Comment

Philipp Vogel
Philipp Vogel on 14 Jan 2021
Hello Mr. Fangjun Jiang,
thank you for your model proposal.
Regards,
Philipp

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!