I've specified axis limits manually, why do they resize during animation?

44 views (last 30 days)
I'm animating uploaded data from a GUI. The animation is a trail of data that adds new values as dark blue and old values trail off into white, they translate across S from 1->0 in HSV color space. When I run the animation from the GUI - I'm using the App Editor - the axis limits gets recalculated every frame of the animation, it's awful. But they work fine when I'm not using the GUI. Do I need to set the axes limits another way?
I specify axis limits using axis as follows
axis(1.1*[-boardWid/2 boardWid/2 -boardHei/2 boardHei/2])
Also, my code, the one that does work (from a script)
figure(1), clf
boardWid = 600;
boardHei = 400;
numtrail = 15; % amount of trailing markers
t = linspace(0,2*pi);
datax = boardWid/2 * cos(t);
datay = boardHei/2 * sin(t);
hsvcmap = [0.6*ones(numtrail,1), linspace(1,0,numtrail)', ones(numtrail,1)];
ax = scatter(datax(1:numtrail), datay(1:numtrail), [], hsv2rgb(hsvcmap), 'filled');
axis(1.1*[-boardWid/2 boardWid/2 -boardHei/2 boardHei/2])
daspect([1 1 1]) % specify axes ratio to agree with board in visual space
%%play/make the animation
%%maybe we want to make a movie instead
for frame = (numtrail+1):length(datax)
ax.XData = [datax(frame) ax.XData(1:end-1)];
ax.YData = [datay(frame) ax.YData(1:end-1)];
drawnow
pause(0.01)
end
and the code that doesn't is embedded in the mess of the app files, but here's the function that implements the animation routine.
function results = generateTrailingScatterAnimation(app, datax, datay, numtrail)
boardWid = 600;
boardHei = 400;
cla(app.UIAxes);
hsvcmap = [0.6*ones(numtrail,1), linspace(1,0,numtrail)', ones(numtrail,1)];
ax = scatter(app.UIAxes, datax(1:numtrail), datay(1:numtrail),...
[], hsv2rgb(hsvcmap), 'filled');
axis(1.1*[-boardWid/2 boardWid/2 -boardHei/2 boardHei/2])
daspect([1 1 1]) % specify axes ratio to agree with board in visual space
%%play/make the animation
%%maybe we want to make a movie instead
for frame = (numtrail+1):length(datax)
ax.XData = [datax(frame) ax.XData(1:end-1)];
ax.YData = [datay(frame) ax.YData(1:end-1)];
drawnow
pause(0.01)
end
end

Accepted Answer

Image Analyst
Image Analyst on 6 Nov 2017
MATLAB automatically adjusts the axes each time you plot or add to it. To use fixed axes, you can put hold on once they are what you like,
hold on;
or you can use the xlim() and ylim() functions.
xlim([xLow, xHigh]);
ylim([yLow, yHigh]);

More Answers (1)

yeungor
yeungor on 6 Nov 2017
Figured it out how to solve the problem. But I don't know why it works, so that would be nice to get explained. If I use this,
set(app.UIAxes, 'XLim', 1.1*[-boardWid/2 boardWid/2])
set(app.UIAxes, 'YLim', 1.1*[-boardHei/2 boardHei/2])
Instead of using the axis function, then it works, but I don't understand why this is the case. Why won't axis default to the axis in the GUI window? It's the only axes on the figure and the app only has the one figure.
  1 Comment
Alex Baum
Alex Baum on 1 Dec 2018
Near as I can tell those commands directly set the XLim and YLim properties of the axes instead of going through the axis function.

Sign in to comment.

Categories

Find more on Animation 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!