Holding multiple axes' plots in Matlab GUI: - matlab

I'm currently developing a GUI (programatically. No GUIDE has been used) for a project and I need to place 11 axes on the same GUI. I'm using the axes command to get the handles of the 11 controls:
h.AXES_ALL(1)=axes('parent',h.fig,'position',[L1 T W H]);
h.AXES_ALL(2)=axes('parent',h.fig,'position',[L2 T W H]);
h.AXES_ALL(3)=axes('parent',h.fig,'position',[L3 T W H]);
...
They all have the same dimensions and I'm using the for instruction to plot the data:
for i=1:11
set(h.PLOT(i),'parent',h.AXES_ALL(i),'XData',x_data,'YData',y_data);
end
But the problem is that the last plot (the 11th) is the one that is shown on the axes control (the 11th) and all the other axes are empty. My objective is to plot 11 curves on 11 different axes controls. They aren't located in the same position, just for the record.
THanks in advance!
Charlie

You said in your comment that you start with a single axes handle:
ha = axes;
And you try to create two plots with the same parent axes, but it does not work as you intended:
>> h.PLOT(1:2) = plot(ha,0,0)
h.PLOT =
195.0035 195.0035
That just replicated the same plot series handle. So, when you go to set the plot data and parent axes for each plot, you are just moving the plot from axes to axes, updating the data while you go.
Use the plot command in a loop, using the appropriate axes handle for each plot:
for ip=1:11,
h.PLOT_ALL(ip) = plot(h.AXES_ALL(ip),...);
end
Then when you update the plot's XData and YData as you want to do, you do not have to change the parent axes.

Related

How to update a scatter3 plot (in a loop) in Matlab

Quite a simple question but just couldn't find the answer online... I want to visualise a point cloud gathered from a lidar. I can plot the individual frames but wanted to loop them to create a "animation". I know how to do it for normal plots with drawnow but can't get it working with a scatter3. If I simply call scatter3 again like I have done in the commented code then the frame that I am viewing in the scatter plot jumps around with every update (Very uncomfortable). How do i get the scatter3 plot to update to the new points without changing the UI of the scatter ie. Still be able to pan and zoom around the visualised point cloud while it loops through.
EDIT: The file is a rosbag file, I cannot attach it because it is 170MB. The problem doesn't happen when using scatter3 in a loop with a normal array seems to be something with using scatter3 to call a PointCloud2 type file using frame = readMessages(rawBag, i).
EDIT: The problem does not seem to be with the axis limits but rather with the view of the axis within the figure window. When the scatter is initialised it is viewed with the positive x to the right side, positive y out of the screen and positive z upwards, as shown in view 1. Then after a short while it jumps to the second view, where the axis have changed, positive x is now out of the screen, positive y to the right and positive z upwards (both views shown in figures). This makes it not possible to view in a loop as it is constantly switching. So basically how to update the plot without calling scatter3(pointCloudData)?
rawBag = rosbag('jackwalking.bag');
frame = readMessages(rawBag, 1);
scatter3(frame{1});
hold on
for i = 1:length(readMessages(rawBag))
disp(i)
frame = readMessages(rawBag, i);
% UPDATE the 3D Scatter %
% drawnow does not work?
% Currently using:
scatter3(frame{1})
pause(.01)
end
The trick is to not use functions such as scatter or plot in an animation, but instead modify the data in the plot that is already there. These functions always reset axes properties, which is why you see the view reset. When modifying the existing plot, the axes are not affected.
The function scatter3 (as do all plotting functions) returns a handle to the graphics object that renders the plot. In the case of scatter3, this handle has three properties of interest here: XData, YData, and ZData. You can update these properties to change the location of the points:
N = 100;
data = randn(N,3) * 40;
h = scatter3(data(:,1),data(:,2),data(:,3));
for ii = 1:500
data = data + randn(N,3);
set(h,'XData',data(:,1),'YData',data(:,2),'ZData',data(:,3));
drawnow
pause(1/5)
end
The new data can be totally different too, it doesn't even need to contain the same number of points.
But when modifying these three properties, you will see the XLim, YLim and ZLim properties of the axes change. That is, the axes will rescale to accommodate all the data. If you need to prevent this, set the axes' XLimMode, YLimMode and ZLimMode to 'manual':
set(gca,'XLimMode','manual','YLimMode','manual','ZLimMode','manual')
When manually setting the limits, the limit mode is always set to manual.
As far as I understood what you describe as "plots jumpying around", the reason for this are the automatically adjusted x,y,z limits of the scatter3 plot. You can change the XLimMode, YLimMode, ZLimMode behaviour to manual to force the axis to stay fixed. You have to provide initial axes limits, though.
% Mock data, since you haven't provided a data sample
x = randn(200,50);
y = randn(200,50);
z = randn(200,50);
% Plot first frame before loop
HS = scatter3(x(:,1), y(:,1), z(:,1));
hold on
% Provide initial axes limits (adjust to your data)
xlim([-5,5])
ylim([-5,5])
zlim([-5,5])
% Set 'LimModes' to 'manual' to prevent auto resaling of the plot
set(gca, 'XLimMode', 'manual', 'YLimMode', 'manual', 'ZLimMode', 'manual')
for i=2:len(x,2)
scatter3(x(:,i), y(:,i), z(:,i))
pause(1)
end
This yields an "animation" of plots, where you can pan and zoom into the data while continuous points are added in the loop

Plotting in GUI of Matlab

Until now i had only 1 axis in my GUI to I used to just plot directly using plot command. Plus i need to plot these in a loop.
for i = 1:length(sig)
plot(sig(i).time,sig(i).signal,sig(i).time,updated(i).filter,)
hold on
end
Now i have 2 axes in my GUI, how can I make a certain plot appear in 1st axis and another in my 2nd axis
Now for example i need to plot the below in the 2nd axis
for i = 1:length(sig)
plot(sig(i).time,sig(i).fil,sig(i).time,updated(i).sig,)
hold on
end
Any help will be appriciated
You could specify the axes for hold and plot functions. Considering you have two axes, h1 and h2 inside your figure, you could do the following:
hold(h1, 'on')
hold(h2, 'on')
for i = 1:length(sig)
plot(h1, sig(i).time,sig(i).signal,sig(i).time,updated(i).filter)
plot(h2, sig(i).time,sig(i).fil,sig(i).time,updated(i).sig)
end

MATLAB: a better way to switch between primary and secondary y-axis?

I'm aware of plotyy but in my opinion it's not as intuitive as for example typing subplot(2,3,1) and from that point one working in that particular subplot's environment...
Suppose I have the following data:
a=rand(20,1);
a_cumul=cumsum(a);
I would like to do a plot of a_cumul on the primary (left hand) y-axis and a bar chart of a on the secondary (right hand) y-axis.
I'm well aware that I can do:
plotyy(1:length(a_cumul),a_cumul,1:length(a),a,'plot','bar')
But this is cumbersome and what if I want to for example plot to the secondary y-axis only and not plot to the primary y-axis? In short, I'm looking for whether a solution like this exists:
figure;
switchToPrimaryYAxis; % What to do here??
plot(a_cumul);
% Do some formatting here if needed...
switchToSecondaryYAxis; % What to do here??
bar(a);
Thanks a lot for your help!
Basically plotyy:
creates two superimposed axes
plots the data specified as the first two params on the first axes
plots the data specified as the last two params on the second axes
set the second second axes color to none making it "transparent" so allowing seeing the graph on the first axes
moves the yaxislocation from the standard position (left) to right
You can create a figure, then two axes make make any plot on the two axes by selecting then with axes(h) where h is the handler of the axes.
Then you can write a your own function performing the axes adjustment.
Script to create figure, axes and call the function to adjust the axes
% Generate example data
t1=0:.1:2*pi;
t2=0:.1:4*pi;
y1=sin(t1);
y2=cos(t2);
% Create a "figure"
figure
% Create two axes
a1=axes
a2=axes
% Set the first axes as current axes
axes(a1)
% Plot something
plot(t1,y1,'k','linewidth',2)
% Set the second axes as current axes
axes(a2)
% Plot something
plot(t2,y2,'b','linewidth',2)
grid
% Adjust the axes:
my_plotyy(a1,a2)
Function to adjust the axes - emulating plotyy behaviour
The function requires, as input, the handles of the two axes
function my_plotyy(a1,a2)
set(a1,'ycolor',[0 0 0])
set(a1,'box','on')
% Adjust the second axes:
% change x and y axis color
% move x and y axis location
% set axes color to none (this make it transparend allowing seeing the
% graph on the first axes
set(a2,'ycolor','b')
set(a2,'xcolor','b')
set(a2,'YAxisLocation','right')
set(a2,'XAxisLocation','top')
set(a2,'color','none')
set(a2,'box','off')
Hope this helps.

Overlapping axes in GUI when plotting boxplot in MATLAB

I am creating a GUI in MATLAB using GUIDE. I have several axes, and in one of them I want to draw a boxplot. My problem is that after drawing the boxplot, the size of the axes changes, and it overlaps with some of my other figures.
To replicate this problem, create a .fig file using GUIDE containing two axes: axes1 and axes2, as shown in the figure: .
Then, in the OpeningFcn, add the following lines:
Z = normrnd(1,3,[100,1]);
plot(handles.axes1, Z);
boxplot(handles.axes2,Z)
Then lauch the GUI. I see the following:
As you can see, the two axes overlap. I've tried changing the properties of the box plot, but with no luck.
I use MATLAB 7.10 (R2010a) and Kubuntu 12.10.
It appears that boxplot makes the axes grow wider, not sure why. In any case, saving the axes position right before plotting and resetting it right after seems to work for me:
Z = normrnd(1,3,[100,1]);
plot(handles.axes1, Z);
pos = get(handles.axes2, 'position');
boxplot(handles.axes2,Z);
set(handles.axes2, 'position', pos);
Cheers,
Giuseppe

MATLAB - How to zoom subplots together?

I have multiple subplots in one figure. The X axis of each plot is the same variable (time). The Y axis on each plot is different (both in what it represents and the magnitude of the data).
I would like a way to zoom in on the time scale on all plots simultaneously. Ideally by using the rectangle zoom tool on one of the plots, and having the other plots change their X limits accordingly. The Y limits should remained unchanged for all of this. Auto fitting the data to fill the plot in the Y direction is acceptable.
(This question is almost identical to Stack Overflow question one Matplotlib/Pyplot: How to zoom subplots together? (except for MATLAB))
Use the built-in linkaxes function as follows:
linkaxes([hAxes1,hAxes2,hAxes3], 'x');
For more advanced linking (not just the x or y axes), use the built-in linkprop function
Use linkaxes as Yair and Amro already suggested. Following is a quick example for your case
ha(1) = subplot(2,1,1); % get the axes handle when you create the subplot
plot([1:10]); % Plot random stuff here as an example
ha(2) = subplot(2,1,2); % get the axes handle when you create the subplot
plot([1:10]+10); % Plot random stuff here as an example
linkaxes(ha, 'x'); % Link all axes in x
You should be able to zoom in all the subplots simultaneously
If there are many subplots, and collecting their axes handle one by one does not seem a clever way to do the job, you can find all the axes handle in the given figure handle by the following commands
figure_handle = figure;
subplot(2,1,1);
plot([1:10]);
subplot(2,1,2);
plot([1:10]+10);
% find all axes handle of type 'axes' and empty tag
all_ha = findobj( figure_handle, 'type', 'axes', 'tag', '' );
linkaxes( all_ha, 'x' );
The first line finds all the objects under figure_handle of type "axes" and empty tag (''). The condition of the empty tag is to exclude the axe handles of legends, whose tag will be legend.
There might be other axes objects in your figure if it's more than just a simple plot. In such case, you need to add more conditions to identify the axes handles of the plots you are interested in.
To link a pair of figures with linkaxes use:
figure;imagesc(data1);
f1h=findobj(gcf,,’type’,’axes’)
figure;imagesc(data2);
f2h=findobj(gcf,,’type’,’axes’)
linkaxes([f1h,f2h],’xy’)