MATLAB Answers

How can I add a border around a figure window (not just the current axis)?

1 view (last 30 days)
Vincent Bell
Vincent Bell on 2 Dec 2020
Answered: Image Analyst on 2 Dec 2020
I want to add a border window around a figure window and not just the plot area. I want the box to include xlabel, ylabel, and title of the plot plus other text in the figure sides. Below is a copy of what I want boxed:
x = 0:pi/100:10*pi;
y = -1 + 2*cos(x);
f = figure;
f.WindowState = 'maximize';
f.Color = 'white';
plot(x,y);
f.CurrentAxes.Position=[0.1 0.1 0.85 0.85];
grid on;
xlabel('X','FontSize',16)
ylabel('Y','FontSize',16)
title('Trial','FontSize',20)
text([-3.5 31.5],[1.1 -3.3],'Graph of -1 + 2*cos(x)','Color','r','FontSize',18)
saveas(f,'PIC','png');

Answers (1)

Image Analyst
Image Analyst on 2 Dec 2020
Try this:
x = 0:pi/100:10*pi;
y = -1 + 2*cos(x);
f = figure;
f.WindowState = 'maximize';
f.Color = 'white';
plot(x,y);
f.CurrentAxes.Position=[0.1 0.1 0.85 0.85];
grid on;
xlabel('X','FontSize',16)
ylabel('Y','FontSize',16)
title('Trial','FontSize',20)
text([-3.5 31.5],[1.1 -3.3],'Graph of -1 + 2*cos(x)','Color','r','FontSize',18)
% Shrink the axes.
ax = gca;
ax.Units = 'normalized';
ax.Position = [.1, .1, .8, .8];
% Add a red frame to the figure.
f.Units = 'normalized';
lineWidth = 50; % Whatever you want.
annotation('line', [0, 0], [0, 1], 'LineWidth', lineWidth, 'Color', 'r');
annotation('line', [1, 1], [0, 1], 'LineWidth', lineWidth, 'Color', 'r');
annotation('line', [0, 1], [0, 0], 'LineWidth', lineWidth, 'Color', 'r');
annotation('line', [1, 0], [1, 1], 'LineWidth', lineWidth, 'Color', 'r');
msgbox('Done!');

Community Treasure Hunt

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

Start Hunting!