Automatic choice of graphics format file for print command

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?

 Réponse acceptée

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 commentaires

It's not really a lookup table. Rather, it's indexing.
@Walter Roberson: Thank you for your answer. I already know that export_fig is an improved version of the print command and they has similar syntax. However, I want to change the line print(ftmp,fmt,FileName,'-r200');
by
print -r200 fmt FileName;
without getting an error and using the syntax "print argument1 ... argument n" of the print command. Can you tell how I can do this?
@Fangjun Jiang: Thank you for your comment.
If you want to use the "print" command in command-line format but with varying content for the options, you would have to dynamically construct the command and execute it. That is not something I can show you how to do while keeping my self-respect. It is not an accident that the documentation for print() does not show any examples of command line use.
Using the function form of print() is the correct way to go.
You might perhaps want to move the '-r200' option to before the FileName in the print() call, but I do not know if that would make any difference.
@Walter Roberson: I do not understand what you mean when you say "That is not something I can show you how to do while keeping my self-respect". Perhaps my question is too easy to answer (I am asking a stupid question?)
Some things are just too dishonorable to show to someone else! It would be the end of your innocence, and you would be the worse for it.
Where can I learn to construct dynamically a command in Matlab then?
*Why* is it important? Using the functional syntax will always work (except for a few commands such as "global"), and does not have any of the ugliness or risks associated with dynamically constructed commands. Dynamically constructed commands are a primary source of security holes, and usually make the code much harder to assess properly. They are worse by far than using global variables: they are even worse by far than using GOTO. They should be reserved for cases where the language offers no alternative.
@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.
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...

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Printing and Saving dans Centre d'aide et File Exchange

Produits

Tags

Aucun tag saisi pour le moment.

Community Treasure Hunt

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

Start Hunting!

Translated by