error in figure print

4 views (last 30 days)
mado
mado on 19 Jun 2022
Commented: Voss on 3 Jul 2022
I am trying to run this code but i get error message
% if figname set, use it; otherwise the data file name
if isfield(cfg, 'figname') && ~isempty(cfg.figname),
set(gcf, 'Name', sprintf('Fig.%d: %s', gcf, cfg.figname), 'NumberTitle', 'off');
elseif isfield(cfg, 'datafile') && ~isempty(cfg.datafile),
slashpos = find(cfg.datafile == '/' | cfg.datafile == '\', 1, 'last');
if ~isempty(slashpos), cfg.datafile = cfg.datafile(slashpos+1:end); end;
set(gcf, 'Name', sprintf('Fig.%d: %s', gcf, cfg.datafile), 'NumberTitle', 'off');
else % shorten the figure name anyway
set(gcf, 'Name', sprintf('Fig.%d', gcf), 'NumberTitle', 'off');
end;
what does this mean?
"Error using sprintf Function is not defined for 'matlab.ui.Figure' inputs"

Accepted Answer

Walter Roberson
Walter Roberson on 19 Jun 2022
You have two calls to sprintf in which you ask to format gcf
Change those two gcf to be double(gcf)
  2 Comments
mado
mado on 19 Jun 2022
you mean this
set(double(gcf), 'Name', sprintf('Fig.%d: %s', gcf, cfg.datafile), 'NumberTitle', 'off');
it give the same error
Voss
Voss on 3 Jul 2022
cfg = [];
% if figname set, use it; otherwise the data file name
if isfield(cfg, 'figname') && ~isempty(cfg.figname),
set(gcf, 'Name', sprintf('Fig.%d: %s', double(gcf), cfg.figname), 'NumberTitle', 'off');
elseif isfield(cfg, 'datafile') && ~isempty(cfg.datafile),
slashpos = find(cfg.datafile == '/' | cfg.datafile == '\', 1, 'last');
if ~isempty(slashpos), cfg.datafile = cfg.datafile(slashpos+1:end); end;
set(gcf, 'Name', sprintf('Fig.%d: %s', double(gcf), cfg.datafile), 'NumberTitle', 'off');
else % shorten the figure name anyway
set(gcf, 'Name', sprintf('Fig.%d', double(gcf)), 'NumberTitle', 'off');
end
get(gcf,'Name')
ans = 'Fig.1'

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Identification in Help Center and File Exchange

Tags

Products


Release

R2015a

Community Treasure Hunt

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

Start Hunting!