Matlab axes scaling

meu picture meu · Nov 3, 2010 · Viewed 21.8k times · Source

how exactly do you get fixed scaling of axes in Matlab plot when plotting inside a loop? My aim is to see how data is evolving inside the loop. I tried using axis manual and axis(...) with no luck. Any suggestions?

I know hold on does the trick, but I don't want to see the old data.

Answer

gnovice picture gnovice · Nov 3, 2010

If you want to see your new plotted data replace the old plotted data, but maintain the same axes limits, you can update the x and y values of the plotted data using the SET command within your loop. Here's a simple example:

hAxes = axes;                     %# Create a set of axes
hData = plot(hAxes,nan,nan,'*');  %# Initialize a plot object (NaN values will
                                  %#   keep it from being displayed for now)
axis(hAxes,[0 2 0 4]);            %# Fix your axes limits, with x going from 0
                                  %#   to 2 and y going from 0 to 4
for iLoop = 1:200                 %# Loop 100 times
  set(hData,'XData',2*rand,...    %# Set the XData and YData of your plot object
            'YData',4*rand);      %#   to random values in the axes range
  drawnow                         %# Force the graphics to update
end

When you run the above, you will see an asterisk jump around in the axes for a couple of seconds, but the axes limits will stay fixed. You don't have to use the HOLD command because you are just updating an existing plot object, not adding a new one. Even if the new data extends beyond the axes limits, the limits will not change.