[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

[OT] $DISPLAY screwing up with EPS when matlab run in background?




(I posted this in comp.soft-sys.matlab but got no response, so I am trying my luck here)


Hi,

(matlab version: (version 6.5.1.199709 (R13) Service Pack 1)  )
I am trying to run a matlab function I wrote in the background. The function plots the color histogram of a given image, opens the plot window and prints out the figure to an EPS file. When I run the function from within a matlab session, everything works fine. But when I run it in background, problem arises in the eps file generated.

I have a bunch of images to make histograms of. So I wrote a shell script which calls matlab in the background and runs the function over all the images. However, I do not get a proper EPS file. The resulting file is just tiny little white square. The output file of the background session does not report any errors.

Here is the shell script I am using (only one image file is used below, but I will eventually use a for-do-done statement to go over all the images)
#############################################################
#!/bin/bash -f

# Clear the DISPLAY.
#unset DISPLAY  # unset DISPLAY for some shells

# Call MATLAB with the appropriate input and output,
# make it immune to hangups and quits using ''nohup'',
# and run it in the background.
IMG="girl1.png"
MFUNC="colorhistplot('girl1.png')"
nohup matlab -nodisplay -nodesktop -nosplash -r $MFUNC > script.out &
#############################################################

And here is the matlab function that I wrote(The function works for a color PNG file. Can be called as colorhistplot('imgfile.png')):
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function OutArgs=colorhistplot(ImgFile)

%colorhistplot('Img.ppm') Plot color histogram of the specified image file
%
% Img.ppm Filename of the image Img.ppm. Output is Img.eps.
%
%
%
%This function plots the color histogram of the specified file. The output is
%fixed so that the EPS exported can be included in a Word or LaTeX document
%
% Example, call the function in matlab with the following command:
%   colorhistplot('myimg.ppm')
%where myimg.ppm is the color image whose histogram is desired. The output is
%is an EPS file of the same basename as the input image file but with the
%extension ".eps".


%first read the image file data into a matrix
  I = imread(ImgFile);


if (size(I, 3) ~= 3)
    error('rgbhist:numberOfSamples', 'Input image must be RGB.')
end

nBins = 256;

rHist = imhist(I(:,:,1), nBins);
gHist = imhist(I(:,:,2), nBins);
bHist = imhist(I(:,:,3), nBins);

hFig = figure;
figure(hFig);

subplot(1,3,1); hr = bar(0:255, rHist,'r');
%xlabel('i','FontName','Helvetica','FontSize',9);
set(gca,'FontSize',6);axis tight;legend('Red');legend(gca,'boxoff');
ylabel('n','FontName','Helvetica','FontSize',8);


subplot(1,3,2); hg = bar(0:255, gHist,'g');
%ylabel('n','FontName','Helvetica','FontSize',8);
set(gca,'FontSize',6);axis tight;legend('Green');legend(gca,'boxoff');
xlabel('i','FontName','Helvetica','FontSize',9);set(gca,'FontSize',6);axis tight

subplot(1,3,3); hb = bar(0:255, bHist,'b');
%xlabel('i','FontName','Helvetica','FontSize',9);
set(gca,'FontSize',6);axis tight;legend('Blue');legend(gca,'boxoff');
%ylabel('n','FontName','Helvetica','FontSize',8);


%before printing the figure to a file, makeup a filename
%first, find the index where the dot and extension (e.g. .png) starts
ExtInd=regexpi(ImgFile,'\.[a-zA-Z][a-zA-Z][a-zA-Z]$');
%get the basename of the file (without the extension)
BaseName=ImgFile(1:ExtInd-1);
%now make the actual output filename (with the .eps extension)
OutFileName=strcat(BaseName,'.eps')

set(gcf,'PaperUnits','centimeters','PaperPosition',[0.5 0.5 18.5 5.5]);

%now print the figure to EPS figure.
print(gcf,'-depsc2','-r600',OutFileName)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Any idea what is going wrong when the shell script is run? Does this have anything to do with a matlab plot window being open when the print command is to be given?

thanks,
->HS

--
Please remove the underscores ( the '_' symbols) from my email address to obtain the correct one. Apologies, but the fudging is to remove spam.



Reply to: