Error Saving figure: Error using sprintf: Conversion to text from matlab.ui.Figure is not possible.

I want to save a figure containing a tiled layout with images, but the following error occurs every time.
"Error using sprintf: Conversion to text from matlab.ui.Figure is not possible. "
Here is some simplified version of the figure:
load('trees.mat')
fig = figure();
tlo = tiledlayout(fig,1,2,"TileSpacing","compact");
nexttile(tlo);
imshow(X,map);
title('A')
nexttile(tlo);
imshow(X,map);
title('B')
I already tried the savefig function and the saveas function.

4 Comments

Can you show the code where you tried to save the figure (e.g., using savefig or saveas)?
Which line of code gives you the error? And have you copy and pasted the full error message (i.e. all of the red text) ?
It would be better if you can share the code you are working with, as important details about the code seems to have been lost in simplifying it.
This is the full error:
Error using sprintf
Conversion to text from matlab.ui.Figure is not possible.
Error in printf (line 8)
disp(sprintf(str, varargin{:}))
Error in savefig (line 63)
printf('WARN %s: ignoring bad names "%s" "%s"', ...
Error in myfigure (line 14)
savefig(fig, 'test.fig')
The code I am working with contains a huge amount of data and functions I wrote myself, but I broke everything down to this example, which yields exactly the same error.
So I don't think providing the full code would be helpful.
The code, where I tried to save just contains the following additional line:
savefig(fig, 'test.fig')

Sign in to comment.

 Accepted Answer

Your full error message references line 63 in the savefig function, which in the savefig function included in MATLAB:
which -all savefig
/MATLAB/toolbox/matlab/graphics/objectsystem/savefig.m
dbtype 63 savefig.m
63 % throw the warning
does not match the line you showed. When you run the first command I ran on your machine, what is the result? I'm guessing you wrote your own savefig.m or downloaded one from somewhere else that's taking precedence over the one included in MATLAB, especially since:
which -all printf
'printf' not found.
there is no printf function in MATLAB.

2 Comments

You are completely correct. I used a toolbox which, unbeknownst to me, contained a savefig.m.
Removing the path to the toolbox solved the problem.
Many thanks!
It may be worth contacting the author of the toolbox about this. They may want to consider renaming their functions to avoid such confusion.

Sign in to comment.

More Answers (0)

Categories

Find more on Printing and Saving in Help Center and File Exchange

Products

Release

R2021b

Asked:

on 31 Oct 2023

Commented:

on 2 Nov 2023

Community Treasure Hunt

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

Start Hunting!