export_fig problem with linebreak in title
14 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I try to export my figure as pdf with export_fig. Some of the titles have linebreaks.
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;
% Layout-Parameter
tile_size_cm = 4.0; % image size in cm
n_cols = 3;
n_rows = 2;
padding_cm = 0.8;
spacing_cm = 0.3;
colorbar_width_cm = 0.3; % colorbar width
fig_width_cm = padding_cm*2 + n_cols*tile_size_cm + (n_cols-1)*spacing_cm + colorbar_width_cm + 0.3; % extra rechts
fig_height_cm = padding_cm*2 + n_rows*tile_size_cm + (n_rows-1)*spacing_cm;
f = figure('Units', 'centimeters', ...
'Position', [5 5 fig_width_cm fig_height_cm], ...
'PaperUnits', 'centimeters', ...
'PaperSize', [fig_width_cm fig_height_cm], ...
'PaperPositionMode', 'manual', ...
'PaperPosition', [0 0 fig_width_cm fig_height_cm]);
cm2norm_w = @(x) x / fig_width_cm;
cm2norm_h = @(x) x / fig_height_cm;
imgs1 = {img_sos, img_oar_pixel, img_oar_block};
titles1 = {sprintf('Sum-of-Squares'), ...
sprintf('Adaptive Kombination\n(Pixelweise)'), ...
sprintf('Adaptive Kombination\n(Blockweise)')};
clim1 = [0 1];
cmap1 = jet;
for k = 1:3
row = 2; col = k; % upper row = row 2
left = cm2norm_w(padding_cm + (col-1)*(tile_size_cm + spacing_cm));
bottom = cm2norm_h(padding_cm + (n_rows-row)*(tile_size_cm + spacing_cm));
ax1(k) = axes('Position', [left bottom cm2norm_w(tile_size_cm) cm2norm_h(tile_size_cm)]);
imagesc(imgs1{k}, clim1);
axis image off;
colormap(ax1(k), cmap1);
end
cb1_left = cm2norm_w(padding_cm + n_cols*tile_size_cm + (n_cols-1)*spacing_cm + 0.1);
cb1_bottom = cm2norm_h(padding_cm + tile_size_cm + spacing_cm);
cb1_height = cm2norm_h(tile_size_cm);
cb1 = colorbar('Position', [cb1_left cb1_bottom cm2norm_w(colorbar_width_cm) cb1_height]);
cb1.Label.String = 'Normierte Signalintensit\"at';
cb1.Label.Interpreter = 'latex';
cb1.Label.FontSize = 10;
cb1.Ticks = 0:0.2:1;
imgs2 = {snr_sos, snr_oar_pixel, snr_oar_block};
clim2 = [0 snr_max_50];
cmap2 = hot;
for k = 1:3
row = 1; col = k; % bottom row = row 1
left = cm2norm_w(padding_cm + (col-1)*(tile_size_cm + spacing_cm));
bottom = cm2norm_h(padding_cm + (n_rows-row)*(tile_size_cm + spacing_cm));
ax2(k) = axes('Position', [left bottom cm2norm_w(tile_size_cm) cm2norm_h(tile_size_cm)]);
imagesc(imgs2{k}, clim2);
axis image off;
colormap(ax2(k), cmap2);
title_k = titles1(k);
title(cell2str(title_k), 'FontSize', 10, 'Interpreter', 'latex', ...
'FontName', 'Latin Modern Roman');
end
cb2_left = cb1_left;
cb2_bottom = cm2norm_h(padding_cm);
cb2_height = cm2norm_h(tile_size_cm);
cb2 = colorbar('Position', [cb2_left cb2_bottom cm2norm_w(colorbar_width_cm) cb2_height]);
cb2.Label.String = '$\mathrm{SNR}$';
cb2.Label.Interpreter = 'latex';
cb2.Label.FontSize = 10;
cb2.Ticks = 0:50:snr_max_50;
export_fig('Test_Vergleich.pdf', '-pdf', '-transparent');
But when I do this I get the following error:
Error using horzcat
Inconsistent concatenation dimensions because a 1-by-3 'char' array was converted to a 1-by-1 'cell' array. Consider creating arrays of the same type
before concatenating.
Error in print2eps (line 605)
fstrm = regexprep(fstrm, '(%%Title:)[^\n]*\n', ['$1 ' title_str '\n']);
^^^^^
Error in export_fig (line 1146)
print2eps(tmp_nam, fig, options, printArgs{:}); %winopen(tmp_nam)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in Copy_of_Vergleich_Mehrkanalkombination (line 99)
export_fig('Test_Vergleich.pdf', '-pdf', '-transparent');
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Without linebreak it works.
2 commentaires
Matt J
le 14 Juil 2025
I get a different error,
Warning: Error in state of SceneNode.
String scalar or character vector must have valid interpreter syntax:
Normierte Signalintensit\\"at
> In defaulterrorcallback (line 12)
In matlab.graphics.illustration/ColorBar/get.Label
In test (line 55)
Unrecognized function or variable 'cell2str'.
Error in test (line 74)
title(cell2str(title_k), 'FontSize', 10, 'Interpreter', 'latex', ...
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Rik
le 14 Juil 2025
Unfortunately, this code can't run online either, even with a cell2str stub function.
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;
% Layout-Parameter
tile_size_cm = 4.0; % image size in cm
n_cols = 3;
n_rows = 2;
padding_cm = 0.8;
spacing_cm = 0.3;
colorbar_width_cm = 0.3; % colorbar width
fig_width_cm = padding_cm*2 + n_cols*tile_size_cm + (n_cols-1)*spacing_cm + colorbar_width_cm + 0.3; % extra rechts
fig_height_cm = padding_cm*2 + n_rows*tile_size_cm + (n_rows-1)*spacing_cm;
f = figure('Units', 'centimeters', ...
'Position', [5 5 fig_width_cm fig_height_cm], ...
'PaperUnits', 'centimeters', ...
'PaperSize', [fig_width_cm fig_height_cm], ...
'PaperPositionMode', 'manual', ...
'PaperPosition', [0 0 fig_width_cm fig_height_cm]);
cm2norm_w = @(x) x / fig_width_cm;
cm2norm_h = @(x) x / fig_height_cm;
imgs1 = {img_sos, img_oar_pixel, img_oar_block};
titles1 = {sprintf('Sum-of-Squares'), ...
sprintf('Adaptive Kombination\n(Pixelweise)'), ...
sprintf('Adaptive Kombination\n(Blockweise)')};
clim1 = [0 1];
cmap1 = jet;
for k = 1:3
row = 2; col = k; % upper row = row 2
left = cm2norm_w(padding_cm + (col-1)*(tile_size_cm + spacing_cm));
bottom = cm2norm_h(padding_cm + (n_rows-row)*(tile_size_cm + spacing_cm));
ax1(k) = axes('Position', [left bottom cm2norm_w(tile_size_cm) cm2norm_h(tile_size_cm)]);
imagesc(imgs1{k}, clim1);
axis image off;
colormap(ax1(k), cmap1);
end
cb1_left = cm2norm_w(padding_cm + n_cols*tile_size_cm + (n_cols-1)*spacing_cm + 0.1);
cb1_bottom = cm2norm_h(padding_cm + tile_size_cm + spacing_cm);
cb1_height = cm2norm_h(tile_size_cm);
cb1 = colorbar('Position', [cb1_left cb1_bottom cm2norm_w(colorbar_width_cm) cb1_height]);
cb1.Label.String = 'Normierte Signalintensit\"at';
cb1.Label.Interpreter = 'latex';
cb1.Label.FontSize = 10;
cb1.Ticks = 0:0.2:1;
imgs2 = {snr_sos, snr_oar_pixel, snr_oar_block};
clim2 = [0 snr_max_50];
cmap2 = hot;
for k = 1:3
row = 1; col = k; % bottom row = row 1
left = cm2norm_w(padding_cm + (col-1)*(tile_size_cm + spacing_cm));
bottom = cm2norm_h(padding_cm + (n_rows-row)*(tile_size_cm + spacing_cm));
ax2(k) = axes('Position', [left bottom cm2norm_w(tile_size_cm) cm2norm_h(tile_size_cm)]);
imagesc(imgs2{k}, clim2);
axis image off;
colormap(ax2(k), cmap2);
title_k = titles1(k);
title(cell2str(title_k), 'FontSize', 10, 'Interpreter', 'latex', ...
'FontName', 'Latin Modern Roman');
end
cb2_left = cb1_left;
cb2_bottom = cm2norm_h(padding_cm);
cb2_height = cm2norm_h(tile_size_cm);
cb2 = colorbar('Position', [cb2_left cb2_bottom cm2norm_w(colorbar_width_cm) cb2_height]);
cb2.Label.String = '$\mathrm{SNR}$';
cb2.Label.Interpreter = 'latex';
cb2.Label.FontSize = 10;
cb2.Ticks = 0:50:snr_max_50;
export_fig('Test_Vergleich.pdf', '-pdf', '-transparent');
function s=cell2str(c)
%placeholder function to make the rest of the code run
s=cell2mat(reshape(c,1,[]));
end
Réponses (1)
Ronit
le 16 Juil 2025
Modifié(e) : Ronit
le 16 Juil 2025
You can replace 'cell2str' with direct access to the cell array like:
title(titles1{k}, 'FontSize', 10, 'Interpreter', 'latex', ...
'FontName', 'Latin Modern Roman');
Additionally, the function 'export_fig' is not a built-in MATLAB function, it is a third-party add-on. I would recommend using the following alternative functions:
- 'exportgraphics': Available for MATLAB R2020a and newer versions.
exportgraphics(f, 'Test_Vergleich.pdf', 'ContentType', 'vector', 'BackgroundColor', 'none');
- 'print': Exports figures to PDF in vector format.
print(f, 'Test_Vergleich.pdf', '-dpdf', '-vector');
After making the above changes:
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;
% Layout-Parameter
tile_size_cm = 4.0; % image size in cm
n_cols = 3;
n_rows = 2;
padding_cm = 0.8;
spacing_cm = 0.3;
colorbar_width_cm = 0.3; % colorbar width
fig_width_cm = padding_cm*2 + n_cols*tile_size_cm + (n_cols-1)*spacing_cm + colorbar_width_cm + 0.3; % extra rechts
fig_height_cm = padding_cm*2 + n_rows*tile_size_cm + (n_rows-1)*spacing_cm;
f = figure('Units', 'centimeters', ...
'Position', [5 5 fig_width_cm fig_height_cm], ...
'PaperUnits', 'centimeters', ...
'PaperSize', [fig_width_cm fig_height_cm], ...
'PaperPositionMode', 'manual', ...
'PaperPosition', [0 0 fig_width_cm fig_height_cm]);
cm2norm_w = @(x) x / fig_width_cm;
cm2norm_h = @(x) x / fig_height_cm;
imgs1 = {img_sos, img_oar_pixel, img_oar_block};
titles1 = {sprintf('Sum-of-Squares'), ...
sprintf('Adaptive Kombination\n(Pixelweise)'), ...
sprintf('Adaptive Kombination\n(Blockweise)')};
clim1 = [0 1];
cmap1 = jet;
for k = 1:3
row = 2; col = k; % upper row = row 2
left = cm2norm_w(padding_cm + (col-1)*(tile_size_cm + spacing_cm));
bottom = cm2norm_h(padding_cm + (n_rows-row)*(tile_size_cm + spacing_cm));
ax1(k) = axes('Position', [left bottom cm2norm_w(tile_size_cm) cm2norm_h(tile_size_cm)]);
imagesc(imgs1{k}, clim1);
axis image off;
colormap(ax1(k), cmap1);
end
cb1_left = cm2norm_w(padding_cm + n_cols*tile_size_cm + (n_cols-1)*spacing_cm + 0.1);
cb1_bottom = cm2norm_h(padding_cm + tile_size_cm + spacing_cm);
cb1_height = cm2norm_h(tile_size_cm);
cb1 = colorbar('Position', [cb1_left cb1_bottom cm2norm_w(colorbar_width_cm) cb1_height]);
cb1.Label.String = 'Normierte Signalintensit\"at';
cb1.Label.Interpreter = 'latex';
cb1.Label.FontSize = 10;
cb1.Ticks = 0:0.2:1;
imgs2 = {snr_sos, snr_oar_pixel, snr_oar_block};
clim2 = [0 snr_max_50];
cmap2 = hot;
for k = 1:3
row = 1; col = k; % bottom row = row 1
left = cm2norm_w(padding_cm + (col-1)*(tile_size_cm + spacing_cm));
bottom = cm2norm_h(padding_cm + (n_rows-row)*(tile_size_cm + spacing_cm));
ax2(k) = axes('Position', [left bottom cm2norm_w(tile_size_cm) cm2norm_h(tile_size_cm)]);
imagesc(imgs2{k}, clim2);
axis image off;
colormap(ax2(k), cmap2);
title(titles1{k}, 'FontSize', 10, 'Interpreter', 'latex', ...
'FontName', 'Latin Modern Roman');
end
cb2_left = cb1_left;
cb2_bottom = cm2norm_h(padding_cm);
cb2_height = cm2norm_h(tile_size_cm);
cb2 = colorbar('Position', [cb2_left cb2_bottom cm2norm_w(colorbar_width_cm) cb2_height]);
cb2.Label.String = '$\mathrm{SNR}$';
cb2.Label.Interpreter = 'latex';
cb2.Label.FontSize = 10;
cb2.Ticks = 0:50:snr_max_50;
% print(f, 'Test_Vergleich.pdf', '-dpdf', '-vector');
exportgraphics(f, 'Test_Vergleich.pdf', 'ContentType', 'vector', 'BackgroundColor','none');
Please refer to the following documentation pages for more information about the functions:
- 'exportgraphics': https://www.mathworks.com/help/matlab/ref/exportgraphics.html
- 'print': https://www.mathworks.com/help/matlab/ref/print.html
I hope the above recommendations help you resolve the query.
0 commentaires
Voir également
Catégories
En savoir plus sur Printing and Saving dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

