- Part 1: Where setup of the entire graph is placed
- Part 2: Where actual changing of parameter and plotting of graph is placed
Why is it so slow to draw a figure in Matlab?
14 views (last 30 days)
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?
Prateek Rai on 3 Jun 2022
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:
It will look like:
%% 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;
%% this represents Numeric Slider in Live Task
y = sin(k*x);
% Plot the signal versus time:
xlabel('time (in seconds)');
title('Signal versus Time');
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.