How to insert text below the images in matlab.

I want text to be displayed below the image (in figure window) as shown with colors. How can this be done. Anyone can help me out in this.??

 Accepted Answer

Try this
im = imread('peppers.png');
imshow(im);
annotation('textbox', [0.13 0.08 0.8 0.1], ...
'String', 'What: This is an image containing oranges', ...
'Color', [1 0.5 0], ...
'FontWeight', 'bold', ...
'EdgeColor', 'none')
annotation('textbox', [0.13 0.03 0.8 0.1], ...
'String', 'Benefit: Good for health', ...
'Color', [0 0.5 0], ...
'FontWeight', 'bold', ...
'EdgeColor', 'none')

7 Comments

I am glad to be of help.
A small change is required again. Help me. "What:" and "Benefit:" are always fixed text for all images, where as the later part, keeps changing for each image.
This is an image containing vegetables.
Good for health. (say these two srtings are in two variables, str1 and str2)
How can I alter this.??.
Try this code
im = imread('peppers.png');
imshow(im);
str1 = 'This is an image containing vegetables.';
str2 = 'Good for health.';
annotation('textbox', [0.13 0.08 0.8 0.1], ...
'String', ['What: ' str1], ...
'Color', [1 0.5 0], ...
'FontWeight', 'bold', ...
'EdgeColor', 'none')
annotation('textbox', [0.13 0.03 0.8 0.1], ...
'String', ['Benefit: ' str2], ...
'Color', [0 0.5 0], ...
'FontWeight', 'bold', ...
'EdgeColor', 'none')
Working Perfect. Thank you..
thanks. then, how can i change the size and location the image in this case?
You can set the axes 'position' property to move the axes within the figure. If there are other images/objects in the same axes, you can set the 'xdata' and 'ydata' properties of the image object (im) to adjust the displayed extents of the image within the axes coordinate space.
im = imread('peppers.png');
imshow(im);
str1 = 'is an image? ';
str2 = 'miserable little pile of peppers!';
annotation('textbox', [0.13 0.08 0.8 0.1], ...
'String', ['What: ' str1], ...
'Color', [1 0.5 0], ...
'FontWeight', 'bold', ...
'EdgeColor', 'none')
annotation('textbox', [0.13 0.03 0.8 0.1], ...
'String', ['A: ' str2], ...
'Color', [0 0.5 0], ...
'FontWeight', 'bold', ...
'EdgeColor', 'none')
% make the image smaller and move it northeast
axscale = 0.5; % axes scale (normalized)
os = [0.1 0.1]; % axes offset [x y] (normalized)
hax = gca;
sc = 0.5*(1-axscale)*[1 1];
set(hax,'position',[sc+os 1-2*sc])
Bear in mind that the annotations are located in figure coordinates. They aren't positioned relative to the image itself. In this example, the axes position is relative to the figure, so even though the height and width in the 'position' property of the axes are equal, the axes is only square if the figure is square. Also note that the image does not necessarily have the aspect ratio necessary to fill the axes.
All of this adds up to mean that you're going to have to deal with a lot of coordinate wrangling if you want your annotations to always appear neatly next to an image without there being big gaps or overlaps as the image aspect ratio changes.

Sign in to comment.

More Answers (0)

Products

Release

R2019a

Asked:

on 6 May 2020

Edited:

DGM
on 14 Jan 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!