Animated line and moving markers with different, changing colors
20 views (last 30 days)
Show older comments
Edited: Julian Holst on 29 Aug 2022
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
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
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)
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.
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)
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!