savefig error in text

1 view (last 30 days)
Shinobido
Shinobido on 27 Mar 2019
Commented: Jan on 27 Mar 2019
Hello,
I am trying to save a plot using the savefig function, however whenever I do this I got the following error:
Error using matlab.graphics.axis.Axes/set While setting the 'FontSize' property of 'Axes': Value not a numeric scalar
function savefig(filename,textSize,titleSize,res,Title,type,inches)
% saveFig(filename,textFontSize,titleFontSize,resolution,Title,type,inches)
% type = 4 for png, 1 = eps
warning('OFF', 'all')
orient portrait
% SO YOU DONT LOSE AN AXIS!
subplot('Position', [.98 .98 .01 .01]),axis off
if ~isempty(textSize)
all_axis = findall(gcf, 'Type', 'Axes');
set(all_axis, 'Color', 'w', 'XColor', 'k', 'YColor', 'k', ...
'FontSize', textSize, 'XGrid', 'off', 'YGrid', 'off');
end
if ~isempty(titleSize)
all_text = findall(gcf, 'Type', 'Text');
set(all_text, 'FontSize', titleSize, 'FontName', 'Arial');
end
if ~isempty(Title)
subplot('Position', [.1 .97 .8 .01])
text(.25, 1, Title, 'FontSize', round(titleSize))
end; axis off; box off;
if isempty(inches)
inches = [17 10];
end
% SET PAPER SIZE
set(gcf, 'papersize', [inches(1) inches(2)])
set(gcf, 'paperposition', [.25 .25 inches(1)-.5 inches(2)-.5])
all_axis = findall(gcf, 'Type', 'Axes'); set(all_axis, 'Linewidth', 3); % all axes
if type == 1
theFilename = strcat(filename, '.eps');
eval(['print -f1 -r', num2str(res), ' -loose -dpsc2 -painters ''', theFilename, ''';']);
elseif type == 2
theFilename = strcat(filename, '.bmp');
eval(['print -r', num2str(res), ' -loose -dbmp -noui ''', theFilename, ''';']);
elseif type == 3
theFilename = strcat(filename, '.tif');
print('-dtiff', ['-r' num2str(res)], theFilename)
elseif type == 4
theFilename = strcat(filename, '.png');
eval(['print -r', num2str(res), ' -loose -dpng -noui ''', theFilename, ''';']);
elseif type == 5
theFilename = strcat(filename, '.jpg');
eval(['print -r', num2str(res), ' -loose -djpeg -noui ''', theFilename, ''';']);
elseif type == 20
theFilename = strcat(filename, '.fig');
savefig(theFilename);
elseif type == 10
theFilename = strcat(filename, '.pdf');
eval(['print -r', num2str(res), ' -loose -dpdf -painters ''', theFilename, ''';']);
end

Answers (1)

Jan
Jan on 27 Mar 2019
The error message is clear: At least one of the provided values is not a numerical scalar. So how doyou call this function? Check the values of textSize and titleSize.
Your eval commands are ugly. Avoid eval in general.
resStr = sprintf('r%d', res);
switch type
case 1
print('-f1', resStr, '-loose', '-dpsc2', '-painters ', [filename, '.eps']);
case 2
print(resStr, '-loose', '-dbmp', '-noui', [filename, '.bmp']);
case 3
print(resStr, '-dtiff', [filename, '.tif']);
case 4
print(resStr, '-loose', '-dpng', '-noui', [filename, '.png']);
case 5
print(resStr, '-loose', '-djepg', '-noui', [filename, '.jpg']);
case 20
savefig([filename, '.fig');
case 10
print(resStr, '-loose', '-dpdf', '-painters', [filename, '.pdf']);
otherwise
fprintf(2, 'Unknown print type: %d\n', type)
end
  2 Comments
Jan
Jan on 27 Mar 2019
[MOVED from section for answers] Shinobido wrote:
Great, thanks for your reply. I am still having the same error though. When I run this function in another machine it does work.
Also, what shall I do with the code you kindly pasted here? Thanks
Jan
Jan on 27 Mar 2019
Of course you have the same error message, if you do not fix your code. I asked you, how you call this function. One of the inputs textSize or titleSize seems to be not a numerical scalar. Fix this.
You can do with the code I have posted what ever you want to. eval is a really bad idea and causes more troubles then it solves. There is always a better solution, because writing strings to be evaulated as code is too prone to bugs.
Omit the evil warning('OFF', 'all') in addition. Matlab's warnings are very useful. Disabling all of them is a really bad idea. Prefer to remove the source of warnings.

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products


Release

R2016a

Community Treasure Hunt

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

Start Hunting!