Sorry for a somewhat simple question. I am trying to generate a figure which displays the same animation but in different subplots. I'm starting simple and primarily focusing on duplicating the plot first.
I was originally thinking of attaching the handle for the subplot to the other plots
afig = figure;
a1{1} = axes('Position',[.01,.2,.2,.2], 'color','none','Xlim',[-10,10],'Ylim',[-10,10]);
a1{2} = a1{1};
a1{2}.Position = [.3,.2,.2,.2];
a1{3} = a1{1};
a1{3}.Position = [.6,.2,.2,.2];
obj = patch('Parent',a1{1},'XData',[1,3,1],'YData',[1,1,3]);
But this just moves the existing plot around as opposed to duplicating it. (on account that I'm still referring to the same object even though it has different names)
I next thought of just recreating the same setup 3 times and then update the animation, looping through the three, but this feels inefficient and computationally intensive.
afig = figure;
a1{1} = axes('Position',[.01,.2,.2,.2], 'color','none','Xlim',[-10,10],'Ylim',[-10,10]);
a1{2} = axes('Position',[.3,.2,.2,.2], 'color','none','Xlim',[-10,10],'Ylim',[-10,10]);
a1{3} = axes('Position',[.6,.2,.2,.2], 'color','none','Xlim',[-10,10],'Ylim',[-10,10]);
obj{1} = patch('Parent',a1{1},'XData',[1,3,1],'YData',[1,1,3]);
obj{2} = patch('Parent',a1{2},'XData',[1,3,1],'YData',[1,1,3]);
obj{3} = patch('Parent',a1{3},'XData',[1,3,1],'YData',[1,1,3]);
Is there a way to call 1 subplot, update that 1 subplot but have it propagate to other subplots?
It really depends on what you want to do in the end, how complex the animations are, and if you can prepare your plots in advance.
First, if there are only a few objects, you could use the linkprop function to link graphics objects' properties:
afig = figure;
a1{1} = axes('Position',[.01,.2,.2,.2], 'color','none','Xlim',[-10,10],'Ylim',[-10,10]);
obj = patch('Parent',a1{1},'XData',[1,3,1],'YData',[1,1,3]);
a1{2} = copyobj(a1{1}, afig);
a1{2}.Position = [.3,.2,.2,.2];
a1{3} = copyobj(a1{1}, afig);
a1{3}.Position = [.6,.2,.2,.2];
linked_objects = [ a1{1}.Children, a1{2}.Children, a1{3}.Children];
property_names = {'XData', 'YData', 'ZData'};
hlink = linkprop(linked_objects, property_names);
for ii = 1:10
obj.XData(1) = ii;
drawnow
pause(0.01)
end
Here, we first create the base plot, then we copy the axes (note that children objects get copied, too, but not callbacks and other properties, see copyboy). We then link properties we may want to change during the animation (note that you could also link the axes' view properties), and then change them in a loop.
Another approach would be to change the object's properties in a main axes in every loop iteration, and copy the main axes' children to the other axes afterwards. This approach may be more costly – as a lot of objects get copied and rendered – but on the other hand, individual properties do not have to be tracked. Here is an example:
afig = figure;
a1{1} = axes('Position',[.01,.2,.2,.2], 'color','none','Xlim',[-10,10],'Ylim',[-10,10]);
obj = patch('Parent',a1{1},'XData',[1,3,1],'YData',[1,1,3]);
a1{2} = copyobj(a1{1}, afig);
a1{2}.Position = [.3,.2,.2,.2];
a1{3} = copyobj(a1{1}, afig);
a1{3}.Position = [.6,.2,.2,.2];
for ii = 1:10
obj.XData(1) = ii;
delete(a1{2}.Children);
delete(a1{3}.Children);
copyobj(a1{1}.Children, a1{2});
copyobj(a1{1}.Children, a1{3});
drawnow
pause(0.01)
end
Finally, it could be an option to use getframe to just capture the rendered image and display it in the copy axes.
Related
I am trying to use matlab's drawpoint to capture some points of interest in an image interactively.
The output of the argument is of images.roi.Point object type.
How can I store the selected points in an array or struct, so I can iterate over many points instead of defining a new variable for each point?
This is my code at the moment, it's functional, however I want to be able to loop over a certain number of points instead of defining different variables manually.
img = imread('test.jpg');
imshow(img)
p1 = drawpoint;
p2 = drawpoint;
p3 = drawpoint;
p4 = drawpoint;
disp('Press a key when selection is finalized!')
pause;
p = [p1.Position; p2.Position; p3.Position; p4.Position];
The reason I'm using drawpoint is that I want to select the points, adjust their position without loosing zooming capability and store all points once finalized.
How can I modify the code to enable iteration over a certain number of points?
Any help would be much appreciated
I don’t know if it is possible to create an array of these objects. I suspect it is possible, but I don’t know exactly what the syntax should look like. You can also use a cell array, as follows:
N = 4; % number of points
pts = cell(N,1);
for ii = 1:N
pts{ii} = drawpoint;
end
pause;
coords = zeros(N,2);
for ii = 1:N
coords(ii,:) = pts{ii}.Position;
end
Is there a (manual or automatic) way of cutting out several parts of the y-axis for certain x-values in a Matlab plot? I found solutions in other programming languages (see 2 links below), but not for Matlab, except for BreakAxis and BreakYaxis on File Exchange, but this only works for one break.
I am also posting my code below, for which I would like to implement it. I would like to have a y-axis break for each yNegData and yPosData, i.e. two breaks, each at [0.3*min(yNegData) 0.7*min(yNegData] and [0.3*max(yPosData) 0.7*max(yPosData].
If you could write it in a way that I could use it for different kinds of plots (not only bar, but also line, for example), that would be very useful.
http://lagrange.univ-lyon1.fr/docs/matplotlib/examples/pylab_examples/broken_axis.html
Using gnuplot, how to 'cut out' usused y-axis regions of the graph
The code:
revenue = ones(100,1);
opex = -1*ones(100,1);
opex(10:15,1) = 3;
data{1} = revenue;
data{2} = opex;
colors = parula(numel(data));
labels = {'Revenue','Opex'};
for i = 1:numel(data)
dataNeg{i} = data{i};
dataNeg{i}(data{i}>0) = 0;
dataPos{i} = data{i};
dataPos{i}(data{i}<0) = 0;
mdata(i) = nnz(dataPos{i}); % was: mean(data{i});
end
[~,posOrder] = sort(mdata,'ascend');
[~,negOrder] = sort(mdata,'descend');
yDataPos = [dataPos{posOrder}];
yDataNeg = [dataNeg{negOrder}];
hold on;
bNeg = bar(yDataNeg,'stack');
bPos = bar(yDataPos,'stack');
for i= 1:numel(data)
set(bNeg(i),'FaceColor',colors(negOrder(i),:))
set(bPos(i),'FaceColor',colors(posOrder(i),:))
end
legend(labels{:});
hold off;
There is this package
https://nl.mathworks.com/matlabcentral/fileexchange/45760-break-y-axis
which works for lines; it has a little example
a=20*rand(21,1)+10;
figure;hold on;
plot(a);
breakyaxis([14 21]);
% hold off %% I guess, inserted by me
However, it does not seem to work as well for bar plots -- if you replace plot(a) by bar(a) in the example above the split stripe does not cover the width of the axes. It could be tweaked I'm sure but that might not be worth the effort.
If you're OK with switching to R you can use gap.barplot() from the plotrix package (see this example) and you can also still switch to gnuplot (see this example).
Literately figured out the last problem as soon as I posted. Was able to fix many of the issues I was having. Now the problem revolves around hgtransform and linkprop. How does one copy the object location and transformation to additional figures. The code below will copy objects from the first axes to the next and animate making all of them move. However it doesn't copy the transformation.
fig = figure();
% create subplots for stim system 3 plate setup
for aa = 1:3
Stimsubfigures{aa} = axes(...
'Position',[((aa*.21)-.2),.2,.2,.2],'color','none');
set(Stimsubfigures{aa},'xLim',[-320,320])
set(Stimsubfigures{aa},'YLim',[-240,240])
set(Stimsubfigures{aa},'Visible','off')
end
axes(Stimsubfigures{1});
for aa = 1:10
Xdata = [1+aa*50,10+aa*50,10+aa*50,1+aa*50];
ObjectTransformation{aa,1} = hgtransform; % Add object to end of transformation list
ObjectList{aa,1} = patch(... % Add object to end of Object list, bind to transformation list
'Parent', ObjectTransformation{aa}, ...
'XData',Xdata, 'YData',[1,1,20,20],...
'Facecolor', [1,0,0], 'EdgeColor', [1,0,0], ...
'visible','on');
ObjectTransformation{aa,1}.Matrix = makehgtform('zrotate',50);
NextStepX{aa,1} = Xdata;
end
tmp = transpose([ObjectList{:}]);
tmptrans = transpose([ObjectTransformation{:}]);
TrialLength = 10;
% copy objects to other figures
copyobj(tmp,Stimsubfigures{2})
copyobj(tmp,Stimsubfigures{3})
property_names = {'XData', 'YData', 'ZData'};
for aa = 1:10
linked_objects = [tmp(aa),...
Stimsubfigures{2}.Children(aa),...
Stimsubfigures{3}.Children(aa)];
hlink{aa} = linkprop(linked_objects,property_names);
end
timer = tic();
while true
t1 = toc(timer);
if t1 >= TrialLength, break;end % break loop once time trial ends
NextStepX = cellfun(#(x) x+1,NextStepX,'UniformOutput',false);
[tmp.XData] = NextStepX{:};
drawnow;
pause(0.1);
step = NextStepX;
end
for aa = 1:3
delete(Stimsubfigures{aa}.Children)
end
When I change this section to copy the transformation, the objects transform correctly but lose the animation.
% copy objects to other figures
copyobj(tmptrans,Stimsubfigures{2})
copyobj(tmptrans,Stimsubfigures{3})
property_names = {'XData', 'YData', 'ZData'};
trans_names = {'zrotate'};
for aa = 1:10
linked_objects = [tmp(aa),...
Stimsubfigures{2}.Children(aa),...
Stimsubfigures{3}.Children(aa)];
Trlink_objects = [tmptrans(aa),...
Stimsubfigures{2}.Children(aa),...
Stimsubfigures{3}.Children(aa)];
hlink{aa} = linkprop(linked_objects,trans_names);
Trhlink{aa} = linkprop(Trlink_objects,trans_names);
end
I tried to perform an copyobj on both handles but it just results in two sets of objects. How can one link all three together so I can perform rotation change Xdata?
Figured it out. Will post answer for those who had similar questions.
The hgtransform is a parent of the objects which are being rotated. because of this when I copy the hgtransform, the children are also copied, hence why the objects appear in the proper orientation in the other windows. From here, I need to link the children from the copied parents to generate the animation.
% copy objects to other figures
copyobj(tmptrans,Stimsubfigures{2})
copyobj(tmptrans,Stimsubfigures{3})
property_names = {'XData', 'YData', 'ZData'};
for aa = 1:10
linked_objects = [tmptrans(aa).Children(1),...
Stimsubfigures{2}.Children(aa).Children(1),...
Stimsubfigures{3}.Children(aa).Children(1)];
hlink{aa} = linkprop(linked_objects,trans_names);
end
By replacing the part of code with the section above. One can transform the objects and animate.
First of all, I just want to say that I'm not that used to using matlab, but I need for an assignment, I'm supposed to create a "brownian movement". My code is currently looking like this:
clf
hold on
prompt = 'Ge ett input';
size = input(prompt) ;
numParticles = input('Ange antal partiklar');
axis([-size size -size size]);
Part = [];
color = 'brkgmyco';
for i = drange(1:numParticles)
Part = [Part [0;0]];
end
for i = drange(1:200)
dxdy = randn(2,numParticles);
k = Part
Part = Part + dxdy;
My concern is how to print, I would even want like a small delay on every print, so you really can see what's happening for the assignment, is this possible to achieve from the code I've written for now or should anything be changed? Thanks in advance!
Here are some basic problems with your code, regardless of what you are trying to do:
You use size as a variable name. Doing so overrides MATLAB's function size.
The function zeros creates an array initialized by zeros, no need for a loop for that.
Instead of calculating randn for 200 times in a loop, you can do it once, with dxdy = randn(2,numParticles,200) and then simply refer to dxdy(:,:,i) within the loop.
The same holds for summation. Instead of summing within a loop to get the cumulative sum, use cumsum like Part = cumsum(randn(2,numParticles,200),3); and then refer to Part(:,:,i), within the loop.
Now to your task. You said you want to know how to print, but I believe you want to plot because you use some commands like axis, clf and hold, that refer to graphic objects. However, you never really do plot anything.
The basic and general function for plotting in 2D is plot, but there are many other more specific functions. One of them is scatter, and it has a sister function gscatter, that takes triples of x, y and groupand plot each (x(k),y(k)) colored by their group(k).
This code plots the particles on an axes, and animate their movement:
prompt = 'Ge ett input';
scope = input(prompt) ;
numParticles = input('Ange antal partiklar');
N = 500;
Part = cumsum(randn(2,numParticles,N)*scope/100,3);
h = gscatter(Part(1,:,1),Part(2,:,1),1:numParticles);
axis([-scope scope -scope scope]);
legend off
for k = 2:N
for p = 1:numParticles
h(p).XData = Part(1,p,k);
h(p).YData = Part(2,p,k);
end
drawnow
end
Is this what you look for?
I don't know how to accomplish the following in MATLAB. I have a figure which looks like this:
In the figure, I have a panel with many subplots and a scrollbar that allows me to view a portion of the panel.
I want to save the whole contents of the panel to a PNG image file (not just the visible portion), i.e. I want to have a file which is a tall rectangle, and doesn't require scrolling.
The code for generating the figure is as follows:
function draw(obj)
figure;
panel1 = uipanel('Parent',1);
panel2 = uipanel('Parent',panel1);
panelheight = obj.nIterations / 2;
set(panel1,'Position',[0 0 0.97 1]);
set(panel2,'Position',[0 1-panelheight 1 panelheight]); %%
nPlot = 1;
for i=1:obj.nIterations
models = obj.iterations{i};
for nModel=1:length(models)
subplot(obj.nIterations,length(models)*2,nPlot);
nPlot = nPlot + 1;
drawTransitions(models{nModel});
set(gca,'Parent',panel2);
subplot(obj.nIterations,length(models)*2,nPlot);
nPlot = nPlot + 1;
drawRewards(models{nModel});
set(gca,'Parent',panel2);
end
end
s = uicontrol('Style','Slider','Parent',1,...
'Units','normalized','Position',[0.97 0 0.03 1],...
'Value',1,'Callback',{#slider_callback1,panel2,panelheight});
end
I have tried the following, without success.
The saveas funstion saves the whole figure, not just the panel. Also, it clips the invisible portion of the panel.
export_fig(panel2.'file.png') gives just a solid gray image.
Why don't you just scroll your panel and grab the frames and concatenate them all together? Here's some code that will basically do that. I would have posted am image, but I guess I don't have enough points for that. You may need to fiddle with the scrolling, and maybe making the slider invisible, but it works.
function printPanel(pnl,filename)
fig = figure(ancestor(pnl,'figure'));
pnl_units = get(pnl,'units');
fig_units = get(fig,'units');
set(pnl,'units','pixels')
set(fig,'units','pixels')
pnl_rect = getpixelposition(pnl);
fig_rect = getpixelposition(fig);
pnl_height = pnl_rect(4);
fig_height = fig_rect(4);
pnl_rect(2) = -pnl_height;
set(pnl,'position',pnl_rect)
N = ceil(pnl_height/fig_height);
CDATA = cell(N,1);
for i = 1:N
F = getframe(fig);
CDATA{i} = F.cdata;
pnl_rect(2) = pnl_rect(2)+fig_height;
set(pnl,'position',pnl_rect)
drawnow
end
set(pnl,'units',pnl_units)
set(fig,'units',fig_units)
imwrite(cat(1,CDATA{:}),filename)
end
You could get rid of the ui elements and just make a figure with all the subplots, and then export that one, using e.g. print -dpng ....
saveas takes a handle as a first argument. Maybe this does not have to be a figure or model handle, but could be a reference to the contents of the panel.