Automatic choice of graphics format file for print command
조회 수: 1 (최근 30일)
이전 댓글 표시
I have the following code:
X = 0:pi/100:2*pi;
Y = sin(X);
fh = figure('toolbar','none','menubar','none','Units','characters');
Pan1 = uipanel(fh,'Units','normalized','Position',[0 0 0.5 1],'title',...
'Panel1');
Pan2 = uipanel(fh,'Units','normalized','Position',[0.5 0 0.5 1],'title',...
'Panel2');
haxes = axes('Parent',Pan2,'Units', 'normalized','Position',...
[0.125 0.1 0.75 0.75]);
hplot = plot(haxes,X,Y);
xlabel(haxes,'Time (second)');
ylabel(haxes,'Amplitude (meter)');
title(haxes,'Sine function');
FileName = uiputfile('*.bmp;*.png;*.jpg;*.tif','Save as');
ftmp = figure('Menu','none','Toolbar','none','Units','normalized',...
'Position',[-1000 -1000 1 1]);
set(gcf,'PaperPositionMode','auto');
set(gcf,'InvertHardcopy','off');
new_axes = copyobj(haxes, ftmp);
set(new_axes,'Units','normalized','Position',[0.1 0.1 0.8 0.8]);
saveas(ftmp, FileName);
delete(ftmp);
delete(fh);
I have two problems:
Number #1: I want the background color of the figure printed to be gray. For this reason, I use the command
set(gcf,'InvertHardcopy','off'); However, when I save the image as a bmp format file, it appears an upper white strip on the image printed. This strip does not appear when the remaining formats (i.e., png, tif and jpg) are used.
Number #2: I want to change the command saveas by print and allow it to select the graphics format file automatically. One possibility is:
[FileName,PathName,FilterIndex] = uiputfile('*.bmp;*.png;*.jpg;*.tif','Save as');
ftmp = figure('Menu','none','Toolbar','none','Units','normalized',...
'Position',[-1000 -1000 1 1]);
set(gcf,'PaperPositionMode','auto');
set(gcf,'InvertHardcopy','off');
new_axes = copyobj(haxes, ftmp);
set(new_axes,'Units','normalized','Position',[0.1 0.1 0.8 0.8]);
switch FilterIndex
case 1 % graphics format file is bmp
fmt = '-dbmp';
case 2 % graphics format file is png
fmt = '-dpng';
case 4 % graphics format file is jpeg
fmt = '-djpeg';
otherwise % graphics format file is tiff
fmt = '-dtiff';
end
print(ftmp,fmt,FileName,'-r200');
delete(ftmp);
delete(fh);
What are the alternatives solutions to my problems? How I could change the line
print(ftmp,fmt,FileName,'-r200');
by
print -r200 fmt FileName;
without getting an error?
댓글 수: 0
채택된 답변
Walter Roberson
2011년 11월 18일
I am a fan of using lookup tables instead of switch statements.
Drivers = {'-dbmp', '-dpng', '-djpeg', '-dtiff'};
fmt = Drivers{FilterIndex};
When I see problems such as white strips for bmp plots, my thoughts immediately turn to "Use Oliver's export_fig". Oliver works hard to get everything right, and if his contribution does not already do it right, he will usually investigate and either repair or describe why the problem occurs.
댓글 수: 10
Walter Roberson
2011년 11월 19일
The mechanism involved is an advanced programming technique that *looks* like a simple technique. It is like diving into water without first checking the area for rocks, broken glass, sharks, alligators, poisonous snakes, dense weeds, muggers, discarded munitions...
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!