create meshed rectangle from its vertex values in matlab - matlab

I am facing some problem in creating the meshed rectangle. I know the max and min coordinate values (xmin, xmax, ymin, ymax, zmin, zmax). So, I can easily find the vertex of the rectangle which are
[xmin ymin zmin;
xmax ymin zmin;
xmax ymax zmin;
xmin ymax zmin;
xmin ymin zmax;
xmax ymin zmax;
xmax ymax zmax;
xmin ymax zmax]
Now how can I create the rectangle with the surface mesh?

You can do this using a patch in which you define vertices and the patch faces. For a rectangular prism, you can create your vertices and faces in the following way:
[xx,yy,zz] = ndgrid([xmin, xmax], [ymin, ymax], [zmin zmax]);
vertices = [xx(:), yy(:), zz(:)];
% Each row corresponds to a face of the prism and the values are indices into vertices
faces = [1 2 6 5
2 4 8 6
4 3 7 8
3 1 5 7
1 2 4 3
5 6 8 7];
p = patch('Faces', faces, ...
'Vertices', vertices, ...
'FaceColor', [0, 0.4470, 0.7410], ...
'FaceAlpha', 0.2); % Set transparency so we can see it

Related

How to shade area and make it transparent between two lines in MATLAB?

I shaded the area between two lines, it's not very clean:
area(xData,[Y1(:) ,Y2(:)-Y1(:)]); hold on
colormap([1 1 1; 0 0 1]);
How to make it transparent too in MATLAB? So that it comes like:
ref:peltiertech.com
You can use the FaceAlpha property of the area object to set the transparency level:
xData = 1:7;
Y1 = [3 2 1 4 3 2 1];
Y2 = [8 6 9 8 7 5 6];
area(xData, Y2, 'EdgeColor',[0 .447 .741], 'FaceColor',[0.929 .694 .125], 'FaceAlpha',.3);
hold on
area(xData, Y1, 'EdgeColor',[0 .447 .741], 'FaceColor', [1 1 1]);
A cleaner approach is to use patch instead of area:
h = patch([xData xData(end:-1:1) xData(1)], [Y1 Y2(end:-1:1) Y1(1)], 'b');
set(h, 'EdgeColor',[0 .447 .741], 'FaceColor',[0.929 .694 .125], 'FaceAlpha',.3)

How to create a colormap based on value?

I have two vectors as follows:
x = 0:5:50;
sir_dB = [50 20 10 5 2 0 -5 -10 -20 -20 -20]
Where x denotes the distance on the x-axis and sir_dB the SNR. For this, I need to generate a color map for a grid of 50 x 60m something similar to this:
based on the value of sir_dB.
I tried the following:
sir_dB = [50 20 10 5 2 0 -5 -10 -20 -20 -20];
xrange = 0:50;
yrange = -30:30;
% create candidate set
[X, Y] = ndgrid(xrange, yrange); % grid of points with a spacing of 1.
candidate_set = [X(:), Y(:)];
test_pt = [0 30];
radius = 5;
% find which of these are within the radius of selected point:
idx = rangesearch(candidate_set, test_pt, radius );
neighborhood = candidate_set(idx{1}, :);
Once I have the neighbors at a radius of 5m, I need to color that part of the grid based on the sir_dB value for a corresponding x value.
I need to have the plot in such a way that for all values of sir_dB greater than 15, the grid should be colored green, yellow for y greater than 0 and red for y greater than -20.
Could someone provide me inputs of how to do this best?
Im not sure exactly what you want, but this should get you started with contourf. I increased the granularity of xrange and yrange to make the radius more smooth but you can change it back if you want.
x = 0:5:50;
sir_dB = [50 20 10 5 2 0 -5 -10 -20 -20 -20];
xrange = 0:0.1:50;
yrange = -30:0.1:30;
% create candidate set
[X, Y] = ndgrid(xrange, yrange); % grid of points with a spacing of 1.
candidate_set = [X(:), Y(:)];
test_pt = [0 30];
r = sqrt((test_pt(1)-X(:)).^2 + (test_pt(2)-Y(:)).^2);
idx = r>5;
snr = nan(size(X));
snr(idx) = interp1(x,sir_dB,X(idx),'linear');
% Some red, yellow, green colors
cmap = [0.8500 0.3250 0.0980;
0.9290 0.6940 0.1250;
0 0.7470 0.1245];
figure();
colormap(cmap);
contourf(X,Y,snr,[-20,0,15],'LineStyle','none');
Plotting the the contour plot alongside the original sir_dB we see that it lines up (assuming you want linear interpolation). If you don't want linear interpolation use 'prev' or 'next' for the interp1 method.
figure();
colormap(cmap);
subplot(2,1,1);
contourf(X,Y,snr,[-20,0,15],'LineStyle','none');
subplot(2,1,2);
plot([0,50],[-20,-20],'-r',[0,50],[0,0],'-y',[0,50],[15,15],'-g',x,sir_dB);
Here is another suggestion, to use imagesc for that. I nothed the changes in the code below with % ->:
x = 0:5:50;
sir_dB = [50 20 10 5 2 0 -5 -10 -20 -20 -20];
xrange = 0:50;
yrange = -30:30;
% create candidate set
[X, Y] = ndgrid(xrange, yrange); % grid of points with a spacing of 1.
% -> create a map for plotting
Signal_map = nan(size(Y));
candidate_set = [X(:), Y(:)];
test_pt = [10 20];
radius = 35;
% find which of these are within the radius of selected point:
idx = rangesearch(candidate_set,test_pt,radius);
neighborhood = candidate_set(idx{1}, :);
% -> calculate the distance form the test point:
D = pdist2(test_pt,neighborhood);
% -> convert the values to SNR color:
x_level = sum(x<D.',2);
x_level(x_level==0)=1;
ColorCode = sir_dB(x_level);
% -> apply the values to the map:
Signal_map(idx{1}) = ColorCode;
% -> plot the map:
imagesc(xrange,yrange,rot90(Signal_map,2))
axis xy
% -> apply custom color map for g-y-r:
cmap = [1 1 1 % white
1 0 0 % red
1 1 0 % yellow
0 1 0];% green
colormap(repelem(cmap,[1 20 15 35],1))
c = colorbar;
% -> scale the colorbar axis:
caxis([-21 50]);
c.Limits = [-20 50];
c.Label.String = 'SNR';
The result:

Plot data ignoring some X-axis values on Matlab

I'm trying to plot some data on Matlab using the following code:
x = [1 2 5 6 7 9]
y1 = [1 2 3 2 1 2]
y2 = [2 2 2 1 3 3]
y3 = [1 1 2 3 1 1]
plot(x,y1,'--.','markersize',20); hold on;
plot(x,y2,'--.','markersize',20); hold on;
plot(x,y3,'--.','markersize',20); hold off;
legend('y1','y2','y3');
xlim([1 9]);
ylim([0 4]);
And I'm getting the following result:
Note that I have no Y values for the X positions 3, 4 and 8, but the X axis is still showing these X values in the graph.
There is some way that I can ignore the positions 3, 4 and 8 in the X-axis, and show only the Y values for the X positions 1, 2, 5, 6, 7 and 9?
I can use the following command to 'hide' these positions:
set(gca, 'XTick', x);
But the gaps related to these positions are still there.
Update:
This is the graph I'm trying to create (it was created on paint):
Note: in my case, the X-axis just represents the IDs of some images, and because of that I just need to show the numbers.
You can get the plot you want by first leaving x out in the calls to plot (so it plots against the array index) then altering the XTick and XTickLabel properties of the axes (and adjusting the x limit slightly):
plot(y1,'--.','markersize',20); hold on;
plot(y2,'--.','markersize',20);
plot(y3,'--.','markersize',20);
legend('y1','y2','y3');
xlim([1 numel(x)]); % Note numel is used here
ylim([0 4]);
set(gca, 'XTick', 1:numel(x), 'XTickLabel', cellstr(num2str(x(:))));
I am not sure if you need gaps between plot lines. If you wish no values to be plotted there, try replacing the values by nan. For example,
x = [1 2 3 4 5 6 7 8 9]
y1 = [1 2 nan nan 3 2 1 nan 2]
y2 = [2 2 nan nan 2 1 3 nan 3]
y3 = [1 1 nan nan 2 3 1 nan 1]
plot(x,y1,'--.','markersize',20); hold on;
plot(x,y2,'--.','markersize',20); hold on;
plot(x,y3,'--.','markersize',20); hold off;
legend('y1','y2','y3');
xlim([1 9]);
ylim([0 4]);
This will give a plot like this:
Sounds like you need stem.
stem(x, y1);
legend('y1');

Matlab circle radius duplication

I'm new to Matlab can anyone explain why the circle is measuring as 10 on the graph double to the radius. I expected it to be 5 matching the radius
xCenter = 5;
yCenter = 5;
theta = 0 : 0.01 : 2*pi;
radius = 5;
x = radius * cos(theta) + xCenter;
y = radius * sin(theta) + yCenter;
plot(x, y);
axis square;
xlim([0 10]);
ylim([0 10]);
grid on;
Thanks
If you want the circle to reach 5 on the x and y axes, then it should be centered at the origin.

Shading a rectangle

Consider the rectangle formed by the vertices (0, 0), (0, 10), (1, 10), and (1, 0). How would I shade it red in MATLAB?
Note. For some reason, none of the mentioned shadings here works.
You can do that for example with patch:
vertices = [0 0; 0 10; 1 10; 1 0];
patch(vertices(1:end,1), vertices(1:end,2), [1 .2 .2], 'edgecolor', [0 0 0]);
%// [1 .2 .2] is light red for the fill; [1 1 1] is black for the edge
axis([-1 2 -10 20]); %// set axis limits to properly see rectangle