Why is it so slow to draw a figure in Matlab?

조회 수: 40 (최근 30일)
smarthu
smarthu 2022년 5월 28일
댓글: dpb 2022년 6월 5일
I just started learning Matlab.
I am drawing very simple function curves, like y=kx or y=sin(fx).
Then I am using a slide bar to change the values of k or f.
In softwares like Geogebra, or program I made using VB.net, the figure will change with the changing values without any delay.
But in Matlab, though the figure does change, there is an obvious delay, it seems the drawing takes a lot of time.
In Matlab, I tried to draw using Script, Live Script, App.
While changing the k or f values, I have tried to update the figure with 'plot' or 'fplot' directly, or to update with 'set' to change the 'yData' or 'Function'.
How can I speed up the drawing in Matlab?
What's causing this slow drawing?
  댓글 수: 12
smarthu
smarthu 2022년 5월 29일
Yes, the line method draws much faster.
Now the figure changes as fast as I move the slider!
Thanks!
dpb
dpb 2022년 5월 30일
Ah, so! I was certain it would help, not so sure it would be a total solution...

댓글을 달려면 로그인하십시오.

답변 (1개)

Prateek Rai
Prateek Rai 2022년 6월 3일
Hi,
To my understanding, you are attempting to draw function curves with a parameter whose value depends on a slide bar.
Here is the possible workaround:
You can create Live Editor in MATLAB and divide the whole code into 2 sections:
  1. Part 1: Where setup of the entire graph is placed
  2. Part 2: Where actual changing of parameter and plotting of graph is placed
It will look like:
Section 1
%% Sine Wave plot
%% Time specifications:
Fs = 8000; % samples per second
dt = 1/Fs; % seconds per sample
StopTime = 0.25; % seconds
t = (0:dt:StopTime-dt)'; % seconds
%% F and x specifications:
Fc = 60;
x = 2*pi*Fc*t;
Section 2
k =
k
-7
-1010
-7
%% this represents Numeric Slider in Live Task
k = -7
y = sin(k*x);
% Plot the signal versus time:
figure;
plot(t,y);
xlabel('time (in seconds)');
title('Signal versus Time');
zoom xon;
By doing so,
If you change the 'k' value using slider, only graph and plot part will get affected and thus will update the graph fast.
On a similar note, if you want to just make a small part of code dynamic then try to keep it in a separate section in live editor to get faster execution.
You can refer to the following MathWorks Documentation page to learn more about MATLAB Live Editor.
  댓글 수: 2
smarthu
smarthu 2022년 6월 5일
I have already tried this method. It's slow compared to Geogebra.
In the comments above, dpb has already pointed out that methods like fplot will be slow.
And I have to use line method to draw the curve.
dpb
dpb 2022년 6월 5일
@smarthu -- did you never try animatedline and addpoints?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by