how to make sgtitle to display the titles at the left side of the montage?

9 次查看(过去 30 天)
sgtitle displays the title at the center and i have a variable which contains multiple titles that has to display horizontally on top instead the titles are tiling up one below the other .. and displayed at the back end of the image and are not visible .plz help me how to display horizontally above the montage or how to place the title at the left side using sgtitle... kindly help .Iam attaching the figure which displays title at the backside of the image.
here is my code
figure('Name','Query-Results');
sgt=sgtitle(Qclass);
sgt.Color='blue';
sgt.FontSize=12;
montage(img(),'size',[numel(Qclass),numImgsPerClass])
and iam attcahing two figures in which how sgtitle has displayed the titles plz check... when there are more classes titles are occluded and not visible.. and all the titles are displayed one below the other... but i wanted it sequentialy one beside the other.. plz help me. or atleast at the left side.

回答(1 个)

Tridib
Tridib 2025-4-22
Hi @hp,
You cannot achieve the exact placement of the title using only “sgtitle” in MATLAB because the “sgtitle” function is used to add a super title to a group of subplots, and it always positions the text at the very top of the figure, above all axes and will stack each string on a new line if you provide a cell array or a string array of multiple titles. sgtitle” does not allow you to control the vertical position of the title beyond the top margin.
To display all class names in a single line, horizontally, on the top, concatenate them with a separator (such as space) and use the annotation function to place the textbox accordingly:
% Class titles
Qclass = {'BUS', 'DINOSAUR', 'ELEPHANT'};
% Number of images in each class
numImgsPerClass = 8;
% Generating random image array containing 24 images of size [5 5 3]
images = rand(5, 5, 3, 24);
figure('Name','Query-Results');
montage(images, 'size', [numel(Qclass),numImgsPerClass], "BorderSize", 3);
% Creating a string by concatenating all the class titles with spaces
% Change the number of spaces to align the class names accordingly
titleStr = strjoin(Qclass, [' ' ...
' ']);
% Adding a textbox annotation at the top
% Change the position and size of the textbox accordingly
annotation('textbox', [0, 0.9, 1, 0.1], ... % [x y w h] in normalized units
'String', titleStr, ...
'Color', 'blue', ...
'FontSize', 12, ...
'EdgeColor', 'none', ...
'HorizontalAlignment', 'center');
To place the titles on the left side, concatenate the class names using line breaks:
% Class titles
Qclass = {'BUS', 'DINOSAUR', 'ELEPHANT'};
% Number of images in each class
numImgsPerClass = 8;
% Generating random image array containing 24 images of size [5 5 3]
images = rand(5, 5, 3, 24);
figure('Name','Query-Results');
montage(images, 'size', [numel(Qclass),numImgsPerClass], "BorderSize", 3);
% Creating a string by concatenating all the class titles with line breaks
% Change the number of line breaks to align the class names accordingly
titleStr = strjoin(Qclass, '\n\n\n\n\n\n\n\n\n\n\n\n\n\n');
% Adding a textbox annotation at the left side
% Change the position and size of the textbox accordingly
annotation('textbox', [0, 0.5, 0.1, 0.3], ... % [x y w h] in normalized units
'String', titleStr, ...
'Color', 'blue', ...
'FontSize', 12, ...
'EdgeColor', 'none', ...
'HorizontalAlignment', 'left');
For more help, refer to the following documentations:
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by