error in figure print

3 次查看(过去 30 天)
mado
mado 2022-6-19
评论: Voss 2022-7-3
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"

采纳的回答

Walter Roberson
Walter Roberson 2022-6-19
You have two calls to sprintf in which you ask to format gcf
Change those two gcf to be double(gcf)
  2 个评论
mado
mado 2022-6-19
you mean this
set(double(gcf), 'Name', sprintf('Fig.%d: %s', gcf, cfg.datafile), 'NumberTitle', 'off');
it give the same error
Voss
Voss 2022-7-3
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'

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Object Identification 的更多信息

标签

产品


版本

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by