Fast updating of live figures using sliders
조회 수: 45 (최근 30일)
이전 댓글 표시
I created a simple live script where sliders are used to select two parameters, and then a step response is plotted, which depends on the two parameters. Here is my simple code
wn = 2; % in live script, this is a slider for picking wn
zeta = 0.2; % in live script, this is a slider for picking zeta
s = tf('s');
step(wn^2/(s^2+2*zeta*wn*s+wn^2),5)
The plot is sluggish to update as I move the sliders, and I want it to be more responsive. I used the "Run on: Value changing" option, but there is still a visible lag. I think the bottleneck is due to the figure being re-drawn every time I change the parameters. Is it possible for the live script to modify an existing figure every time a slider is changed? This might involve having one chunk of code that is only run once per section (e.g. create base figure, axes, labels, initial plot) and then every time the slider is moved, only certain components of the plot are modified, thus speeding up rendering.
If it turns out that "step" is actually the bottleneck here, then I'm open to other suggestions --- e.g. pre-computing all the step responses for all possible parameter values and not doing any live computations. I don't really care how long it takes to pre-compute or pre-compile, I just want the end product to be a truly interactive (and instantly updating) figure.
댓글 수: 3
Mario Malic
2021년 6월 5일
Oh, now I see this, sorry, I don't know about livescript behaviour of figures.
답변 (1개)
Mario Malic
2021년 6월 5일
편집: Mario Malic
2021년 6월 5일
Hi,
This will return the handle to the curve on the plot. You can use its XData and YData property to update the values quickly.
curve = findall(groot, 'type', 'line', 'Tag', 'Curves')
However, the issue with the Slider is that it can obtain values that are unnecessarry to calculate. While you move your slider, it may obtain a lot of different values between i.e. zeta 1e-7 and 2e-7 which probably won't make a notable difference to the plot. So you should come up with a way how to do it. Maybe you could make wn and zeta vector
zeta = 0:1e-2:5;
wn = 0:1e-2:10;
and when the corresponding slider value is within some tolerance of element, calculate and plot the values .
>> clear
>> tic
wn = 2; % in live script, this is a slider for picking wn
zeta = 0.2; % in live script, this is a slider for picking zeta
s = tf('s');
for ii = 1 : 10
step(wn^2/(s^2+2*zeta*wn*s+wn^2),5);
end
toc
Elapsed time is 2.944663 seconds.
This is interesting
>> tic
wn = 2; % in live script, this is a slider for picking wn
zeta = 0.2; % in live script, this is a slider for picking zeta
s = tf('s');
for ii = 1:10
result = step(wn^2/(s^2+2*zeta*wn*s+wn^2),5);
end
toc
Elapsed time is 0.222970 seconds.
Save the output of step into variable, it makes it a lot faster.
Also, this doesn't account the time into updating the plots which might also be significant.
댓글 수: 8
Mario Malic
2021년 6월 6일
The livescript behavior is weird, in the example, two plots are shown, but in fact, there is only a single plot handle. When the properties of the graphic object change, output will be captured, resulting in another plot being shown. I have tried hiding the visibility of the figure, but then no output will be shown when the slider value is changed. I don't know how to do it, or if if it's possible.
참고 항목
카테고리
Help Center 및 File Exchange에서 Specifying Target for Graphics Output에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!