Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Matlab axis, how to update only the data while keeping all the axis properties?

I need to create a movie. Suppose, I create an axis and plot on it something very customized:

figure;
ax = plot(x, y, 'linewidth', 3, 'prop1', value1, 'prop2', value2, ...);
grid minor;
axis(ax, [xmin xmax ymin ymax]);
legend(ax, ...);
xlabel(ax, ...);
ylabel(ax, ...);
title(ax, ...);

Now I run a loop, where only the values of y are being updated.

for k = 1 : N
% y changes, update the axis
end

What is the fastest and easiest way to update the axis with new y (or x and y), keeping all the axis properties?

like image 366
Serg Avatar asked Apr 25 '12 14:04

Serg


People also ask

How do I change the properties of an axis in MATLAB?

To change the units, set the FontUnits property. MATLAB automatically scales some of the text to a percentage of the axes font size. Titles and axis labels — 110% of the axes font size by default. To control the scaling, use the TitleFontSizeMultiplier and LabelFontSizeMultiplier properties.

How do you update a plot on each iteration in MATLAB?

Link the plot to the workspace variables by setting the data source properties of the plotted object to 'x2' and 'y2' . Then, update x2 and y2 in a for loop. Call refreshdata and drawnow each iteration to update the plot based on the updated data.

What does axis tight do in MATLAB?

axis tight sets the axis limits to the range of the data. axis fill sets the axis limits and PlotBoxAspectRatio so that the axes fill the position rectangle.

How do you limit the Z axis in MATLAB?

limmethod = zlim("method") returns the current z-axis limits method, which can be 'tickaligned' , 'tight' , or 'padded' . limmode = zlim("mode") returns the current z-axis limits mode, which is either 'auto' or 'manual' . By default, the mode is automatic unless you specify limits or set the mode to manual.


1 Answers

A fast way is to simply update the y-values of the data you've plotted:

%# note: plot returns the handle to the line, not the axes
%# ax = gca returns the handle to the axes
lineHandle = plot(x, y, 'linewidth', 3, 'prop1', value1, 'prop2', value2, ...);

%# in the loop
set(lineHandle,'ydata',newYdata)

EDIT What if there are multiple lines, i.e. lineHandle is a vector? You can still update in one step; you need to convert the data to a cell array, though.

%# make a plot with random data
lineHandle = plot(rand(12));

%# create new data
newYdata = randn(12);
newYcell = mat2cell(newYdata,12,ones(1,12));

%# set new y-data. Make sure that there is a row in 
%# newYcell for each element in lineH (i.e. that it is a n-by-1 vector
set(lineHandle,{'ydata'},newYcell(:) );
like image 179
Jonas Avatar answered Sep 29 '22 13:09

Jonas