Animated line and moving markers with different, changing colors

10 views (last 30 days)
I want to have 100 markers (hypothesis) that move and change colors based on predetermined values.
I want to use it alongside my animatedline.
Here is an example code I made so you can try it out yourself:
x_est = randn(1,10000)+(0:0.01:99.99);
y_est = randn(1,10000)+(0:0.01:99.99);
x = randn(100,10000)+(0:0.01:99.99); % Hypothesis x-Data
y = randn(100,10000)+(0:0.01:99.99); % Hypothesis y-Data
w = rand(100,10000); % Hypothesis weights
figure(21)
hAL = animatedline; % Handle
hAL.Color = 'r';
handle = gca;
c = [ones(100,1)-w(:,1) zeros(100,1) w(:,1)]; % Colors of each hypothesis
hS = scatter(handle, x(:,1), y(:,1), 6, c, 'filled'); % Show hypothesis
xlim([0 100])
ylim([0 100])
for i = 1:1:10000 % Animate
addpoints(hAL, x_est(i), y_est(i));
c = [ones(100,1)-w(:,i) zeros(100,1) w(:,i)];
set(hS,'xdata', x(:,i), 'ydata', y(:,i),'CData',c)
drawnow;
pause(0.001)
end
I get the following error: "Error using matlab.graphics.animation.AnimatedLine/addpoints Value must be a handle."
Though it works when only using the animatedline or only scatter for the animation. But never when combined.
Do you know how to get rid of this error? Any suggestions for improvements on efficiency are also appreciated.

Accepted Answer

Steven Lord
Steven Lord on 29 Aug 2022
I advise you not to use handle as a variable name, as it already has a meaning in MATLAB.
When you call scatter, since you didn't turn hold on MATLAB will clear all the children from the axes. This includes your animatedline object. To avoid the problem, use hold on to prevent scatter from removing the children.

More Answers (0)

Categories

Find more on Animation in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by