matlab ticks with certain labels - matlab

I have the following part:
set(gca,'ylim',[0 0.3],'ytick',0:0.05:0.3);
set(gca,'xlim',[0 1],'xtick',0:0.05:1);
Ticks must remain as they are since when I enable the grid, ticks give me the grid resolution I want. What I also want is that I want to change the labels of x and y.
I want MATLAB to show all ticks but only show the following ticks' label:
0, 0.2, 0.4, 0.8, 1.0 for x
0, 0.1, 0.2, 0.3 for y
Is there a way to do this?

You can assign labels to ticks using a cell array of strings, where each string corresponds to a tick. For ticks at which you don't want any label, use the empty string:
set(gca,'xticklabel',{'0','','','','0.2','','','','0.4','','','','0.6','','','','0.8','','','','1'})
set(gca,'yticklabel',{'0','','0.1','','0.2','','0.3'})

Related

How to plot a 3d graph in Matlab with my data?

Right now I am doing a parameter sweep and I am trying to convert my data to a 3D graph to show the results in a very nice fashion. The problem is that I don't quite know how to plot it as I am having an issue with the result variable.
mute_rate = [0.5, 0.25, 0.125, 0.0625, 0.03125, 0.015625]
mute_step = linspace(-2.5, 2.5, 6)
results = [949.58, 293.53, 57.69, 53.65, 293.41, 1257.49;
279.19, 97.94, 32.60, 29.52, 90.52, 286.94;
32.96, 28.06, 19.56, 6.44, 13.47, 55.80;
2.01, 1.52, 5.38, 1.00, 0.89, 1.41;
0.61, 0.01, 18.59, 0.03, 0.56, 1.22;
1.85, 1.51, 18.64, 18.57, 18.54, 6.90]
So the first row in the result variable presents the results of the mute rate and mute step performed on the population from my genetic algorithm. For example:
0.5, -2.5 = 949.58,
0.5, -1.5 = 293.53,
0.5, -0.5 = 57.69
etc
It sounds like you want something akin to:
mesh(mute_step, mute_rate, results);
shading interp;
Other styles of plot would be surf or pcolor (for a 2d view).

change the range of the intensity of a color based on a value between zero and one

How can I determine the intensity or ranging the darkness of a color based on probability distribution?
For instance in this code example
pgon1 = [-0.5 -0.6882; 0.5 -0.6882; 0.5 -1.6882;-0.5 -1.6882];
pgon2 = [0.5 -0.6882; 0.806 0.2629; 1.7571 -0.0431;1.4511 -0.9972];
pgon3 = [0.806 0.2629; 0 0.8507; 0.5878 1.6567;1.3938 1.0689];
pgon4 = [0 0.8507; -0.809 0.2629; -1.3968 1.1136;-0.5878 1.6597];
pgon5 = [ -0.809 0.2629; -0.5 -0.6882; -1.4511 -0.9972;-1.7601 -0.0461];
patch(pgon1(:,1),pgon1(:,2),ones(length(pgon1),1),'r')
patch(pgon2(:,1),pgon2(:,2),ones(length(pgon2),1)*2,'k')
patch(pgon3(:,1),pgon3(:,2),ones(length(pgon3),1)*3,'g')
patch(pgon4(:,1),pgon4(:,2),ones(length(pgon4),1)*4,'b')
patch(pgon5(:,1),pgon5(:,2),ones(length(pgon5),1)*5,'m')
axis equal tight
view(3)
grid on
Can the intensity of black or blue be a function of some probability between zero and one?
There are two alternatives:
It can be done using 'FaceColor' Property of the Patch object, with multiplying the RGB Triplet by some intensity value (according to your probabilities) , then you can adjust the darkness of a certain color:
patch(pgon1(:,1),pgon1(:,2),ones(length(pgon1),1)*1,'b','facecolor',0.20*[0, 0 1])
patch(pgon2(:,1),pgon2(:,2),ones(length(pgon2),1)*2,'b','facecolor',0.30*[0, 0 1])
patch(pgon3(:,1),pgon3(:,2),ones(length(pgon3),1)*3,'b','facecolor',0.50*[0, 0 1])
patch(pgon4(:,1),pgon4(:,2),ones(length(pgon4),1)*4,'b','facecolor',0.75*[0, 0 1])
patch(pgon5(:,1),pgon5(:,2),ones(length(pgon5),1)*5,'b','facecolor',1 *[0, 0 1])
OR
By using the 'FaceAlpha' Property, you can adjust the transparency of a certain color according to your probability:
patch(pgon1(:,1),pgon1(:,2),ones(length(pgon1),1)*1,'b','facealpha',1)
patch(pgon2(:,1),pgon2(:,2),ones(length(pgon2),1)*2,'b','facealpha',0.75)
patch(pgon3(:,1),pgon3(:,2),ones(length(pgon3),1)*3,'b','facealpha',0.5)
patch(pgon4(:,1),pgon4(:,2),ones(length(pgon4),1)*4,'b','facealpha',0.3)
patch(pgon5(:,1),pgon5(:,2),ones(length(pgon5),1)*5,'b','facealpha',0.2)

How to change radial tick markers in Matlab polar plot?

I'd like to relabel the radial tick markers in the following polar log plot:
f = figure ;
t = 0:0.01: pi/2 ;
polar(t, 10 * log10(cos(t))/(50) + 1)
from 1, 0.8, 0.6, 0.4, 0.2 to 0, -10, -20, -30, -40 (i.e. radial dB ticks).
Trying some of the methods from Fixing the Radial Axis on MATLAB Polar Plots, I was able to relabel the markers provided my tick markers were positive and increasing.
I tried the following based on How to remove Rho labels from Matlab polar plot?
rho_labels = {'1' '0.8' '0.6' '0.4' '0.2'};
rho_labels2 = {'0' '-10' '-20' '-30' '-40'};
for r=1:length(rho_labels)
ff = findall(f, 'string', rho_labels{r}) ;
ff = rho_labels2{r} ;
end
but it also didn't work (seems to do nothing, so I suspect I'm operating on a copy of the find results not handled).
How can these tick markers be adjusted? Also, if I wanted a different number of concentric circles than 5, how can that be done (for example, 4 subdivisions with -40 dB at the "origin".)
Here is a way to rename the radial tick labels. Be warned that if there is a match between any radial and angular tick mark labels, both will be replaced and the angular labels will be wrong. But the angular tick labels are fixed as 0, 30, 60, ..., 330, so as long as the radial tick labels don't include these values, you should be fine.
What the code does is find all the text in the plot using findall, trim the blank spaces at the front of each string, then, for each entry in rho_labels, set the string entry corresponding to the tick label with that identifier to the corresponding entry in rho_labels2.
f = figure ;
t = 0:0.01: pi/2 ;
polar(t, 10 * log10(cos(t))/(50) + 1)
rho_labels = {'1' '0.8' '0.6' '0.4' '0.2'};
rho_labels2 = {'0' '-10' '-20' '-30' '-40'};
ff = findall(f,'type','text');
t=strtrim(get(ff,'String'));
for r=1:length(rho_labels)
set(ff(strcmp(t,rho_labels{r})),'String',rho_labels2{r})
end
To modify the number of rings, I think think of anything better than modifying polar.m and creating your own custom polarMOD.m function. If you do this you can also do the radial labels as well. In fact, there might be such a function on the MathWorks File Exchange.

Matlab - change axis multiplier

Could you tell me how can I change an axis "multiplier"? I mean a value I circled in the picture, let's say I would like to have x10^3 instead of x10^4.
As of R2015b it is part of the Numeric Ruler Properties:
ax = get(gca);
ax.YAxis.Exponent = -3;
I have little bit tricky solution:
Set YTickMode to manual.
Set your own YTickLabel.
Place the text on top with your desired multiplier.
here is:
set(gca, 'YTickMode', 'manual');
set(gca, 'YTickLabel', get(gca,'YTick') / 1000);
text(0, 1.02 * get(gca,'YLim')(2), 'x 10^3');
Play with the multiplier 1.02 in the third line to place your text to the good place.
You can use the following Matlab Central tick2text: create easy-to-customize tick labels
http://www.mathworks.com/matlabcentral/fileexchange/16003-tick2text-create-easy-to-customize-tick-labels
along with the sprintf formatting.
Scale your data by 0.1, which gives you the multiplier you want.
Then override the tick labels so that tick 1.0 is labelled 10, etc.

Matlab overlaying color on an image

I have a grascale image where I want to overlay different colors to different areas that have similar properties (say direction or intensity etc.) I am not referring to a heat map. Rather I have hard coded segmentation code where I have grouped pixels together by their "similarities". Now I want to over lay colors to those pixels.
For example, for a 3x3 pixel pic, say I know that the top row and bottom row are similar group. And the middle row is another group. How can I overlay a red hue with one group and a blue hue with another?
You could make your 3x3x1 grayscale image into a 3x3x3 color image, and then adjust the hue values for the pixels you want.
So say:
GreyImg=[0.2, 0.3, 0.35;...
0.5, 0.6, 0.56;...
0.8, 0.8, 0.85];
%Convert To Color Img
ColorImg(:,:,1)=GreyImg;
ColorImg(:,:,2)=GreyImg;
ColorImg(:,:,3)=GreyImg;
%Add a red hew to top row:
ColorImg(1:1,:,1)=ColorImg(1:1,:,1)+[.2, .2, .2];
%Add a blew hew to top row:
ColorImg(3:3,:,3)=ColorImg(3:3,:,3)+[.2, .2, .2];
imshow(ColorImg);