matlab work slowly when plot

18 views (last 30 days)
noura
noura on 29 Jul 2024
Commented: noura on 1 Aug 2024
Hello, I'm Having problem Which is when i run my code and there is plot in the code, matlab work very slowly and take more time.
also when i wan't to zoom in or zoom out for the figure it take more than 5 minutes also it hang and the control is very slowly any help ?
the code work fine and no problem with it.
  1 Comment
Steven Lord
Steven Lord on 29 Jul 2024
Without knowing more about what you're doing it's impossible to give any concrete solution. Please show us a small sample of code and data (no more than 10-15 lines of code, ideally, and if necessary a small MAT-file) that you can run (on its own, so we can run it too) and reproduce this behavior.
If I had to guess I'd guess you were adding lots and lots and lots of lines to a figure and all those lines are consuming more and more memory. If you have a hold on call in your code but never call hold off that would support that guess.

Sign in to comment.

Accepted Answer

DGM
DGM on 1 Aug 2024
Edited: DGM on 1 Aug 2024
There are a number of things, but the biggest problem is something easily missed:
plot(r*ones(numtoplot), x(initidx:reps), '.', 'MarkerSize', 1, 'Color', '#be4d25')
For the given parameters, each plotted series is 50 points, and there are 1201 series being plotted. At first glance this might look like what's happening. The ydata is a vector 50 elements long, and it looks like xdata is a constant vector of the same length. The problem is that ones(N) for scalar N will produce a NxN matrix, not a vector. That means that you have 50x as many plot objects than you intend -- 1201x50=60050 line objects in total. That will be very slow to draw or manipulate.
Besides that, we can speed up a lot of things by preallocating and separating the calculations from the plotting routine. Preallocating x instead of growing it repeatedly will greatly speed up calculations for large reps. In the end, we only have one line object in the figure, which helps improve responsiveness of the figure.
interval = [2.8 4.0];
accuracy = 0.001;
reps = 5000; % number of repetitions
numtoplot = 50;
initidx = reps - (numtoplot - 1);
rvec = interval(1):accuracy:interval(2);
nr = numel(rvec);
% calculate everything first
R = repmat(rvec.',[1 numtoplot]);
X = zeros(nr,numtoplot);
for ridx = 1:nr
r = rvec(ridx);
x = zeros(1,numtoplot); % preallocate for speed
x(1) = 0.4;
for n = 2:reps
x(n) = r*x(n-1)*(1 - x(n-1));
end
X(ridx,:) = x(initidx:reps);
end
% then plot everything at once
plot(R(:), X(:), '.', 'MarkerSize', 1, 'Color', '#be4d25')
xlim([2.8 4])
title('Logistic Map')
xlabel('\itr', 'FontSize', 14)
ylabel('\itx', 'FontSize', 14)
Whether there are existing tools for doing these sorts of diagrams, I'm not familiar.
  1 Comment
noura
noura on 1 Aug 2024
that's very helpful! thank you apperciate it

Sign in to comment.

More Answers (1)

Shubham
Shubham on 29 Jul 2024
Hi Noura,
Performance issues with plotting in MATLAB can be caused by several factors, such as the size of the data being plotted, the complexity of the plot, or the rendering options. Here are some tips to improve the performance of your plots and make interactions like zooming and panning more responsive:
Tips to Improve Plot Performance:
  1. Reduce Data Size:
  • If you are plotting a very large dataset, try reducing the number of data points. For example, you can downsample the data before plotting.
% Example of downsampling
factor = 10; % Downsample factor
x = x(1:factor:end);
y = y(1:factor:end);
plot(x, y);
2. Use Efficient Plotting Functions:
  • Use plotting functions that are optimized for performance. For example, plot is generally faster than scatter for large datasets.
  • If you are using scatter, consider using plot if possible.
3. Avoid Unnecessary Plot Updates:
  • If you are updating a plot in a loop, try to minimize the number of updates. Use set to update properties of existing plot objects instead of creating new ones.
h = plot(x, y);
for k = 1:numIterations
% Update plot data
set(h, 'YData', newYData);
drawnow;
end
4. Disable Plot Features:
  • Turn off features that are not necessary for your plot. For example, grid lines, legends, and markers can slow down rendering.
plot(x, y, 'LineWidth', 1); % Avoid markers if not needed
  4 Comments
noura
noura on 31 Jul 2024
Edited: noura on 31 Jul 2024
I'm using a similar code of this code , i found it here
interval = [2.8 4.0];
accuracy = 0.001;
reps = 100; % number of repetitions
numtoplot = 50;
initidx = reps - (numtoplot - 1);
for r = interval(1):accuracy:interval(2)
x = 0.4;
xo = x;
for n = 2:reps
xn = r*xo*(1 - xo);
x = [x xn];
xo = xn;
end
plot(r*ones(numtoplot), x(initidx:reps), '.', 'MarkerSize', 1, 'Color', '#be4d25'),
hold on
end
hold off
xlim([2.8 4])
title('Logistic Map')
xlabel('\itr', 'FontSize', 14)
ylabel('\itx', 'FontSize', 14)
but my N = 5000 which is called reps is the previous code.
my matlab is still slowly i don't know if it because my N = 5000 is very large number, if it is how can i fix the problem @DGM,@Steven Lord
noura
noura on 31 Jul 2024
also i need N to be 5000 or more since i can't get the full diagram if N less than 5000

Sign in to comment.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!