Change width of 2 colorbars in tiledlayout

13 次查看(过去 30 天)
I try to change the width of the colorbars in both r
If I only change it for one of them, it works.
img_sos = rand(64);
img_oar_pixel = rand(64);
img_oar_block = rand(64);
snr_sos = rand(64)*250;
snr_oar_pixel = rand(64)*250;
snr_oar_block = rand(64)*250;
snr_max = ceil(max([snr_sos(:); snr_oar_pixel(:); snr_oar_block(:)]));
snr_max_50 = ceil(snr_max / 50) * 50;
tile_size_cm = 4; % desired size of images
n_cols = 3;
n_rows = 2;
padding_cm = 0.6;
colorbar_width_cm = 1;
% size of figure
fig_width_cm = n_cols * tile_size_cm + (n_cols+1)*padding_cm + colorbar_width_cm;
fig_height_cm = n_rows * tile_size_cm + (n_rows+1)*padding_cm;
f = figure('Units', 'centimeters', 'Position', [5 5 fig_width_cm fig_height_cm]);
tl = tiledlayout(n_rows, n_cols, 'TileSpacing', 'compact', 'Padding', 'compact');
clim1 = [0 1];
cmap1 = jet;
ax1 = [];
ax1(1) = nexttile(1);
imagesc(img_sos, clim1);
axis image off;
title('Sum-of-Squares', 'FontSize', 10, 'Interpreter', 'latex', ...
'FontName', 'Latin Modern Roman');
ax1(2) = nexttile(2);
imagesc(img_oar_pixel, clim1);
axis image off;
title({'Adaptive Kombination', '(Pixelweise)'}, 'FontSize', 10, 'Interpreter', 'latex', ...
'FontName', 'Latin Modern Roman');
ax1(3) = nexttile(3);
imagesc(img_oar_block, clim1);
axis image off;
title({'Adaptive Kombination', '(Blockweise)'}, 'FontSize', 10, 'Interpreter', 'latex', ...
'FontName', 'Latin Modern Roman');
set(ax1, 'Colormap', cmap1);
cb1 = colorbar(ax1(end), 'eastoutside');
cb1.Label.String = 'Normierte Signalintensit\"at';
cb1.Label.Interpreter = 'latex';
cb1.Label.FontSize = 10;
cb1.Ticks = 0:0.2:1;
% THIS IS WHAT I WANT TO DO FOR THE SECOND ONE AS WELL
fixed_cb_width_cm = 0.3;
cb1_pos = cb1.Position;
cb1_pos(3) = fixed_cb_width_cm / fig_width_cm;
cb1.Position = cb1_pos;
clim2 = [0 snr_max_50];
cmap2 = hot;
ax2 = [];
ax2(1) = nexttile(4);
imagesc(snr_sos, clim2);
axis image off;
ax2(2) = nexttile(5);
imagesc(snr_oar_pixel, clim2);
axis image off;
ax2(3) = nexttile(6);
imagesc(snr_oar_block, clim2);
axis image off;
set(ax2, 'Colormap', cmap2);
cb2 = colorbar(ax2(end), 'eastoutside');
cb2.Label.String = 'SNR';
cb2.Label.Interpreter = 'latex';
cb2.Label.FontSize = 10;
cb2.Ticks = 0:50:snr_max_50;
But as soon as I add this part for the second one:
cb2_pos = cb2.Position;
cb2_pos(3) = fixed_cb_width_cm / fig_width_cm;
cb2.Position = cb2_pos;
Warning: Error in state of SceneNode.
String scalar or character vector must have valid interpreter syntax:
Normierte Signalintensit\"at
Both colorbars appear inside the last image ..

回答(1 个)

Star Strider
Star Strider 2025-7-13
编辑:Star Strider 2025-7-13
I am not certain whst result you want.
This seems to work --
img_sos = rand(64);
img_oar_pixel = rand(64);
img_oar_block = rand(64);
snr_sos = rand(64)*250;
snr_oar_pixel = rand(64)*250;
snr_oar_block = rand(64)*250;
snr_max = ceil(max([snr_sos(:); snr_oar_pixel(:); snr_oar_block(:)]));
snr_max_50 = ceil(snr_max / 50) * 50;
tile_size_cm = 4; % desired size of images
n_cols = 3;
n_rows = 2;
padding_cm = 0.6;
colorbar_width_cm = 1;
% size of figure
fig_width_cm = n_cols * tile_size_cm + (n_cols+1)*padding_cm + colorbar_width_cm;
fig_height_cm = n_rows * tile_size_cm + (n_rows+1)*padding_cm;
f = figure('Units', 'centimeters', 'Position', [5 5 fig_width_cm fig_height_cm]);
tl = tiledlayout(n_rows, n_cols, 'TileSpacing', 'compact', 'Padding', 'compact');
clim1 = [0 1];
cmap1 = jet;
ax1 = [];
ax1(1) = nexttile(1);
imagesc(img_sos, clim1);
axis image off;
title('Sum-of-Squares', 'FontSize', 10, 'Interpreter', 'latex', ...
'FontName', 'Latin Modern Roman');
ax1(2) = nexttile(2);
imagesc(img_oar_pixel, clim1);
axis image off;
title({'Adaptive Kombination', '(Pixelweise)'}, 'FontSize', 10, 'Interpreter', 'latex', ...
'FontName', 'Latin Modern Roman');
ax1(3) = nexttile(3);
imagesc(img_oar_block, clim1);
axis image off;
title({'Adaptive Kombination', '(Blockweise)'}, 'FontSize', 10, 'Interpreter', 'latex', ...
'FontName', 'Latin Modern Roman');
set(ax1, 'Colormap', cmap1);
cb1 = colorbar(ax1(end), 'eastoutside');
cb1.Label.String = '$Normierte Signalintensit\\"at$';
cb1.Label.Interpreter = 'latex';
cb1.Label.FontSize = 10;
cb1.FontSize = 4;
cb1.Ticks = 0:0.2:1;
% THIS IS WHAT I WANT TO DO FOR THE SECOND ONE AS WELL
fixed_cb_width_cm = 0.3;
cb1_pos = cb1.Position;
cb1_pos(3) = fixed_cb_width_cm / fig_width_cm;
cb1_pos(1) = cb1_pos(1)+0.05;
cb1_pos(4) = cb1_pos(4)+0.00;
cb1.Position = cb1_pos
cb1 =
ColorBar ($Normierte Signalintensit\\"at$) with properties: Location: 'manual' Limits: [0 1] FontSize: 4 Position: [0.9422 0.5045 0.0195 0.3843] Units: 'normalized' Use GET to show all properties
clim2 = [0 snr_max_50];
cmap2 = hot;
ax2 = [];
ax2(1) = nexttile(4);
imagesc(snr_sos, clim2);
axis image off;
ax2(2) = nexttile(5);
imagesc(snr_oar_pixel, clim2);
axis image off;
ax2(3) = nexttile(6);
imagesc(snr_oar_block, clim2);
axis image off;
set(ax2, 'Colormap', cmap2);
cb2 = colorbar(ax2(end), 'eastoutside');
cb2.Label.String = '$SNR$';
cb2.Label.Interpreter = 'latex';
cb2.Label.FontSize = 10;
cb2.FontSize = 4;
cb2.Ticks = 0:50:snr_max_50;
cb2_pos = cb2.Position;
cb2_pos(1) = cb1_pos(1);
cb2_pos(3) = fixed_cb_width_cm / fig_width_cm;
cb2_pos(4) = cb1_pos(4);
cb2.Position = cb2_pos
cb2 =
ColorBar ($SNR$) with properties: Location: 'manual' Limits: [0 250] FontSize: 4 Position: [0.9422 0.0586 0.0195 0.3843] Units: 'normalized' Use GET to show all properties
For some reason, the first element in each position vector was reset, so I had to calculate those separately here. Change those as necessary to get the result you want.
EDIT -- (13 Jul 2025 st 12:52)
Tweaked 'cb2' to have the same left loccation and width as 'cb1'.
EDIT -- (13 Jul 2025 st 13:22)
Tweaked 'FontSize' properties so colorbar ticks and labels are visible. These may need to be re-tweaked in your findal display of them, because the restrictions to image properties here in the Answers 'Run' feature require different values in order to make them all display correctly.
.
  13 个评论
Star Strider
Star Strider 2025-7-14
Another approach that I thought of later would be to use the figure 'OuterPosition' property (use the gcf function to get its handle) to create a bit more room in the figure width. You would increase only the 'width' (third element in that vector) and leave everything else alone. (I have no way of actually experimenting with this because I do not know what your eventual goal is.) See the 'Figure Properties' documentation for those details, and a description of what 'OuterPosition' refers to.
With respect to the 'export_fig' error, you would need to conbine only string arrays or only cell arrays. You cannot mix them. Do what the error message suggests. (I have never needed to use 'export_fig' so I have no experience with it.)
Instead of 'export_fig', another ooption would be to use the MATLAB exportgraphics function, based on it. It might be easier to use. Check to see if your version of MATLAB supports it.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by