Movement of rectangle with tot function - matlab

How can I make this rectangle move from the top left down to the bottom right?
clear all
close all
clc
n=10;
h=1;
for t=0:n-1
clf;
axis([0 sum(1:n) 0 sum(1:n)]);
axis manual
tot = sum(0:t);
patch([tot tot+(t+1)*h tot+(t+1)*h tot],[tot tot tot+(t+1)*h tot+(t+1)*h],...
[2 6 3 7],'EdgeColor','none','FaceAlpha',(1-(t+1)*.7/n));
pause(1/6)
end
Also, try to use this function somewhere.
sum(1:t)-tot-(t+1)*h

You could compute the Y location for patch function differently, but from your current code, the simplest change will be to reverse the Y axis direction:
axis ij; % put this before patch

You just need to substruct your y coordinates from the total height sum(0:n):
patch([tot tot+(t+1)*h tot+(t+1)*h tot],...
sum(0:n)-[tot tot tot+(t+1)*h tot+(t+1)*h],...
[2 6 3 7],'EdgeColor','none','FaceAlpha',(1-(t+1)*.7/n));

Related

Cannot plot circle with ezplot in matlab

I want to plot 6 circles in figure MATLAB. But it's cannot appear.
I have think of this code is correct, and I try to give axis limits. But it cannot fixing my problem.
clear all;
clc;
p=[8 9 3 4 7 4];
rtopi=[3 4 16 25 34 25];
n=length(p);
for ii=1:n
f=#(x,y)(x-p(ii)).^2+(y).^2-rtopi(ii)^2;
gambar=ezplot(f);
set(gambar,'color','k','linewidth',2);
grid on;
axis equal;
set(gca,'Color','y');
xlabel('Real');
ylabel('Imaginary');
title('Discs');
axis([-30 30 -30 30]);
end
This is the result :
How to fix it?
Remove the .* in the function definition, just use x^2 not x.^2.
Use 'hold on' before end of loop
Move the background color, grid on, title etc. outside the loop.
And most importantly, declare xmin, xmax for the ezplot. Default is (-2pi to 2pi).
Try: gambar = ezplot(f,[xmin,xmax}) and use the plot limits for the xmin and xmax

Rotating a Plot About the Y Axis

I have a vector of values that I want to plot as brightness on a circle through the radius of it (I.e. If it was 0 3 1 5 I'd want a circle that was dark at the centre, then a bright ring around it, then a slightly darker ring, then a brighter ring).
To do this I've attempted to rotate my radial vector (E) around the y axis, as such
[X,Y,Z] = cylinder(E);
h = surf(X,Y,Z),
However I'm clearly not doing it right, as this appears to be rotating my curve around the x axis. I've tried just swapping X and Y, but it still rotates it around the x axis. Any help would be greatly appreciated.
One way would be to rotate your vector and create a surface. The Z data of the surface (your rotated vector) will be color coded according to the colormap you choose, if you display the surface from the top you get your circles at the different brightness.
If you are really only interested from the "top view" of this surface, then no need to create a full surface, a simple pcolor will do the job.
example:
%% // input data (and assumptions)
E=[0 3 1 5 2 7];
nBrightness = 10 ; %// number of brightness levels
r = (0:numel(E)) ; %// radius step=1 by default for consecutive circles
%// otherwise define different thickness for each circle
So if I use stairs([E 0]) you get your different brightness levels:
I had to add a last 0 to the vector to "close" the last level, we'll have to do that again in the solution below.
Now to rotate/replicate that around Y, color code the height, and look at it from the top:
%% // replicate profile around axis
ntt = 50 ; %// define how many angular division for the plot
theta = linspace(0,2*pi,ntt) ; %// create all the angular divisions
[rr,tt]=meshgrid(r,theta) ; %// generate a grid
z = repmat( [E 0] , ntt , 1 ) ; %// replicate our "E" vector to match the grid
[xx,yy,zz] = pol2cart(tt,rr,z) ; %// convert everything to cartesian coordinates
pcolor(xx,yy,zz) %// plot everything
colormap(gray(nBrightness)) %// make sure we use only "nBrightness" colors (Shades of gray)
caxis([0 nBrightness])
shading flat ; axis equal %// refine the view (axis ratio and "spokes" not visible) etc...
colorbar
axis off
will yield the following :
Note that your problem was not fully defined, I had to take assumptions on:
What radius each brightness circle should have ? (I made them all the same but you can modify that)
How many brightness levels you want ? (You can also modify that easily though).
Have you tried the rotate function?
direction = [0 1 0];
rotate(h,direction,90);
In this example a 90 degree rotation is performed around the y axis.
Using this library http://www.mathworks.com/matlabcentral/fileexchange/45952-circle-plotter
%http://www.mathworks.com/matlabcentral/fileexchange/45952-circle-plotter
x0 = 0;
y0 = 0;
colors = [0 3 1 5];
maxC = max(colors);
sz = numel(colors);
for i=fliplr(1:sz)
c = colors(i);
circles(x0,y0,i,'facecolor',[c/maxC c/maxC 0]) % http://au.mathworks.com/help/matlab/ref/colorspec.html
end

How can I align subplot images with different size in matlab

I have plotted three sub-images on a plot with Matlab as Figure 1 shows, you can see their titles and images are not aligned in a line. I want to plot a figure as Figure 2 or 3 where at least the titles are aligned. How can I do that? thank you.
Figure 1
Figure 2
Figure 3
The sort answer is that there is no way (that I know of) to align subplots. You could however manually give the exact positions and size of each image so that means you can compute the aligned locations manually.
It will be something along these lines:
y = .8; % vertical location stays fixed
subplot('Position', [.1 y width1 height1]);
imagesc(img1);
title('1');
[s1 s2] = size(img2);
subplot('Position', [.4 y width2 height2]);
imagesc(img2);
title('2');
[s1 s2] = size(img3);
subplot('Position', [.7 y width3 height3]);
imagesc(img3);
title('3');

How to insert infinity symbol to X axis of Matlab Bar graph?

How can I add the infinity symbol to X axis of Matlab Bar graph?
Naturally it is possible to insert the infinity symbol i.e. '\infty' for xlabel, as seen in the last line of the inserted code.
But, I want to add the infinity sign in the x axis bar not in the x axis label.
How can I do that?
For the sake of detailed clarification, the following script is added bellow:
data=[1 2 3; 1 3 4; 3 1 2];
bar(data)
set(gca,'YLim',[0 3])
set(gca,'YTick',[0:0.5:3])
set(gca, 'YTickLabel',num2str(get(gca,'YTick')','%02.1f%%'))
set(gca,'Xtick',1:3,'XTickLabel',{'\infty' ; '20 dB'; '15 dB'})
xlabel('\infty dB') % x-axis label
How about this solution, using format_tick function from File Exchange?:
data=[1 2 3; 1 3 4; 3 1 2];
bar(data)
set(gca,'YLim',[0 3])
set(gca,'YTick',[0:0.5:3])
set(gca, 'YTickLabel',num2str(get(gca,'YTick')','%02.1f%%'))
set(gca,'Xtick',1:3)
format_ticks(gca, {'$\infty$' ; '20 dB'; '15 dB'})
I left out the xlabel because it interfers with the Xtick, but probably that can be easily moved to lower position.
EDIT:
To fix the overlap of Xtick and xlabel add this to the end of the code:
xlabh = get(gca,'XLabel');
set(xlabh,'Position',get(xlabh,'Position') - [0 .1 0])

Rotate and Move an half-circle Matlab

I plot an half-circle and I rotate it with :
t=linspace(0,pi,1000);
x=r*cos(t);
y=r*sin(t);
h=plot(-2+x,y);
rotate(h,[0 0 -1],1,[0.5 3.71 0]);
now, I would like to move/shif the half-circle from the center , such that the half-circle changes position but I want that it maintains also the rotation done.
Who can help me?
To shift for example 10 units in the x direction and 5 in the y direction:
set(h,'XData',get(h,'XData')+10)
set(h,'YData',get(h,'YData')+5)
As you see, it's done by modifying the properties of the h object
Disregard my above comments, you can actually shift data around after it has been plotted. You can modify the XData and YData values as long as you have a handle to the plot that you created
Do something like this
clc; close all; clear all;
t=linspace(0,pi,1000);
r = 5
x=r*cos(t);
y=r*sin(t);
h=plot(-2+x,y);
rotate(h,[0 0 -1],1,[0.5 3.71 0]);
xShift = 5;
yShift = 5;
set(h,'XData',get(h,'XData')+xShift)
set(h,'YData',get(h,'YData')+yShift)
Just an additional answer to demonstrate hgtransform, the others will work well too.
t=linspace(0,pi,1000);
x=r*cos(t);
y=r*sin(t);
h=plot(-2+x,y);
rotate(h,[0 0 -1],1,[0.5 3.71 0]);
hgt=hgtransform('parent',gca);
set(h,'parent',hgt);
Tx = makehgtform('translate',[10 -5 0]); % translate +10 x, -5 y, 0 z
set(hgt,'Matrix',Tx);
drawnow;
You could also potentially use the hgtranform to do the rotation at the same time, but it might take a bit of fiddling around to get the axis origin to work as you have above:
RxTx = makehgtform(xrotate',0.5,'yrotate',3.71,'translate',[10 -5 0]);
set(hgt,'Matrix',RxTx);
drawnow;