How to draw rectangle around tiled object in a MATLAB plot? - matlab

I created a tiledlayout(4,3)-object and I want to emphasize the 8th tile by a surrounding line or changing the backgroundcolor (not of the graph itself, but just the greyspace behind the graph, labels, axes etc.).
I could not find anything that changes the backgroundcolor of just one tile, so I tried to draw a rectangle around the tile and color it.
I executed the code below and expected to get a red line around the OuterPosition- or the TightInset-position, but nothing happened. The code created the tiles without any error-codes and there was no red rectangle around the 8th tile.
Can anyone tell me how to color the background of a tile or get a rectangle around it?
Thank you very much!
ax = nexttile(8)
lines_around_tile = get(ax, 'OuterPosition'); %'TightInset')
rectangle('Position', lines_around_tile, 'EdgeColor', 'r', 'LineWidth',10)

I got this result.
Result
To draw a rectangle, run the code and in matlab figure (with all the tiles) go to view -> Plot Edit Toolbar. It generates a toolbar where you can find lines, rectangles, circles...
Then insert your rectangle, right click on it, Property Inspector. In this window you can change the facecolor, linecolor, linewidth...

Related

Colorbar properties

I am trying to plot a contour with a colorbar that has a small values (10e-9). This value appears at top of the colorbar. How can I change the location of this value from top to bottom of the colorbar. I want to move this value beneath the colorbar because when I added label above the colorbar it overlapped with this value
I attached image for the figure.
You can accomplish what you want by first finding the handle to the color bar, then changing the yticklabel ticklabels property. This is a cell array of stings, one for each tick mark. You can fill in whatever you want to show there. The multiplier at the top will go away. With the text function you can add your own modifier anywhere you want. But I think it looks nicer within the axis label.
However, the simple solution is to change the units you plot. Multiply everything by 10e9 before plotting, then add a nano prefix to your units.

Ignore certain area(roi) in an image while processing in Matlab

I'm a fresher to Matlab. I'm working on
vision.CascadeObjectDetector on Mat-lab and is used
twice to find-out two different objects(separately
trained), say E and K from a video. bbox and bbox2
are respective ROIs. part of code in while loop is given below:
videoFrame=step(videoFileReader);
bbox=step(letterDetector_E,videoFrame);
bbox2=step(letterDetector_K,videoFrame);
C = vertcat(bbox,bbox2);
videoOut=insertObjectAnnotation(videoFrame, 'rectangle', C, 'E&K');
step(videoPlayer, videoOut);
I want to ignore the area denoted by bbox while finding out bbox2. That is, no two object should be find out from same part of image(obviously there are similiarities in both type of objects). So I wish to mask or ignore bbox area of videoFrame while
bbox2 is assigned with step(letterDetector_K, videoFrame)
is executing. Format of bbox is [xUpperLeft, yUpperLeft, width,
height].
How can I do that.
That's pretty easy to do. Once you detect the first shape, use the bounding box detected for the first object E, then insert a filled rectangle in that spot using insertShape. Make sure you set the Opacity to 1.0 so that it doesn't mix any pixels from the background into this rectangle, and choose a colour of the filled rectangle that is completely different from the object you're trying to detect. Perhaps choose black. Also, I would recommend you turn off anti-aliasing when drawing the rectangle, because it actually takes more time to draw a shape with this turned on. Anti-aliasing essentially smoothes edges for any shapes. Because you don't want to include information with this region, there's really no need for anti-aliasing and so set this to false. This is done with the SmoothEdges option.
Once you're finished filling the shape in, use this modified frame and detect the next object K. As such, you only need one more line of code, so do this:
videoFrame=step(videoFileReader);
bbox=step(letterDetector_E,videoFrame);
%// NEW - Insert filled rectangle
videoFrame = insertShape(videoFrame, 'FilledRectangle', bbox, ...
'Opacity', 1.0, 'Color', 'black', ...
'SmoothEdges', false);
%// Now detect next shape on modified frame
bbox2=step(letterDetector_K,videoFrame);
C = vertcat(bbox,bbox2);
videoOut=insertObjectAnnotation(videoFrame, 'rectangle', C, 'E&K');
step(videoPlayer, videoOut);
How insertShape works is that it takes in an image (videoFrame in your case), then you specify the FilledRectangle flag and the bounding box location, which precisely coincides with a 4 element array in the way you described, which is the x and y of top-left corner, followed by width and height of the rectangle. We will place a filled in rectangle at this location. We then specify further options for the rectangle before drawing it, such as setting the opacity to 1.0, the colour to black and anti-aliasing turned off. We output the modified frame with this rectangle inserted, where I will mutate videoFrame so that there is minimal editing required for your code. You would then use videoFrame to detect the next shape.

How to put in color a certain part of the background in a plot with matlab?

I would like to draw in grey color a certain part of the background in a plot.
I know how to change the background color of the whole plot.I used this code
after the plot function:
HANDLE = gca
get( HANDLE );
set( HANDLE, 'Color', [0.7,0.7,0.7] )
Exemple: y=x^2
How can I do to draw the blue part of the background in grey and to leave the other parts in white?
Thank you in advance
You can always draw a filled 2D rectangle with the desired color:
rectangle('Position',[-5,400,10,400],'FaceColor',[0.7,0.7,0.7])
So:
Put your background in white
Draw the gray rectangle
Finally draw the curve.

How to place axes objects on top of a figure in MATLAB?

I'm now using MATLAB R2012b. I want to draw a grey rectangle together with the axes, and want the rectangle to be behind the axes. I tried
uistack(gca,'top');
but nothing happens.
In this article they mentioned that
However, it is possible to place axes objects on top of UIPANEL
objects (which were introduced in MATLAB 7.0 (R14)).
So I think there should be some way to do that.
Try the following:-
set(gca, 'Layer', 'top')
Ref: Draw Matlab graphs with frame, ticks, on top of graph lines

Display multiple axes on image plot

I have a large plot showing an image. Now, I want to diplay several small bar plots on top of that image, each having its own axes and positioned on a specified position on the image.
I already tried to just append more axes (with transparent background color) to the same figure. This basically works, but If I now zoom in or pan around the background image, the small axes stay on the same position relative to the figure, so they lose the relation to their position on the background image.
Does Matlab offer a better solution?
I recommend drawing the bars using patch command.
For example:
plot(rand(10));
hold on;
patch([1;1;2;2],[1;2;2;1],'r')