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?

채택된 답변

Walter Roberson
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
Julián Francisco
Julián Francisco 2011년 11월 19일
@Walter Roberson: I am not an expert programmer (neither perhaps a programmer). I do not want you to give me an easy answer that I might not understand. My purpose is looking for possible alternatives to do things and one of them has resulted to be the dynamic construction. I do not know the advantages or drawbacks of the dynamic construction of commands because I have never tried it. My only intention is learning to do things that I do not know beforehand.
Walter Roberson
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 CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

제품

Community Treasure Hunt

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

Start Hunting!

Translated by