export_fig problem with linebreak in title

10 次查看(过去 30 天)
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 个评论
Matt J
Matt J 2025-7-14
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
Rik 2025-7-14
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');
For professional Matlab assistance, please contact https://UndocumentedMatlab.com/consulting
Warning: You are using version 3.50 of export_fig. A newer version (3.51) is available, with the following improvements/fixes:
* Fixed: figure with non-default colormap exported with default colormap in some cases (issue #389)
A change-log of recent releases is available here; the complete change-log is included at the top of the export_fig.m file.
You can download the new version from GitHub or Matlab File Exchange, or run export_fig('-update') to install it directly.
Warning: In Matlab R2020a+ you can also use exportgraphics(hFigure,filename,'BackgroundColor','none','ContentType','vector') for simple file export, which could also improve image vectorization, solving rasterization/pixelization problems.
export_fig error. Please ensure: * that the function you used (/users/mss.system.zxvbr/export_fig.m) version 3.5 is from the expected location * and that you did not made a mistake in export_fig's expected input arguments * and that ghostscript is properly installed in Exporting to vector format (EPS, PDF etc.) requires the Ghostscript utility from http://ghostscript.com
Warning: Error in state of SceneNode.
String scalar or character vector must have valid interpreter syntax:
$\mathrm{SNR}$
Error using questdlg (line 71)
Creating dialog boxes that block execution is not supported when MATLAB is in a configuration that is non-interactive or disables window display.

Error in export_fig>selectUtilityPath (line 1633)
answer = questdlg(msg,utilName,'Use local installation','Go to website','Cancel','Cancel');
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in export_fig (line 1584)
selectUtilityPath('Ghostscript',url,'Exporting to vector format (EPS, PDF etc.)');
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in export_fig (line 517)
[imageData, alpha] = export_fig(varargin{:});
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function s=cell2str(c)
%placeholder function to make the rest of the code run
s=cell2mat(reshape(c,1,[]));
end

请先登录,再进行评论。

回答(1 个)

Ronit
Ronit 2025-7-16
编辑:Ronit 2025-7-16
Hello @Dominik,
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');
Warning: Error in state of SceneNode.
String scalar or character vector must have valid interpreter syntax:
$\mathrm{SNR}$
Please refer to the following documentation pages for more information about the functions:
I hope the above recommendations help you resolve the query.

类别

Help CenterFile Exchange 中查找有关 Printing and Saving 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by