Friday, July 8, 2011

[VBA] List folders using VBS

#copyright @ http://scriptdemo.blogspot.com
parentFolder="C:\"
Dim fso, folderOBJ, subF
set fso=CreateObject("Scripting.FileSystemObject")
if fso.folderexists(parentFolder) then
   set folderOBJ=fso.GetFolder(parentFolder)
   For Each subF in folderOBJ.SubFolders
       'WScript.Echo subF.Name
       msgbox("Found " & subF.Name)
   Next
else
   msgbox(parentFolder & " dose not exist")
end if

Sunday, June 26, 2011

[Matlab] create a faked colorbar in matlab

function newhcb=plotcolorbar(hcbar,cmap)
% create a colorbar using true color (fill function) to replace the existing one created by matlab itself
% Usage: newHandle=plotcolorbar(h,cmap)
% copyright @ http://scriptdemo.blogspot.com

if nargin==0
   hcbar=findobj('tag','Colorbar');
   if isempty(hcbar)
      disp('No colorbar obj found!!!'); return;
   end
   cmap=colormap;
elseif nargin==1
   if ishandle(hcbar)
      cmap=colormap;
   else
      cmap=hcbar;
      hcbar=findobj('tag','Colorbar');
      if isempty(hcbar)
         disp('No colorbar obj found!!!'); return;
      end
   end
end
oriNPlot=get(gcf,'NextPlot');
set(gcf,'NextPlot','add');
oriH=gca;
oriPos=get(oriH,'position');

cbarpos=get(hcbar,'position');
cbarxlim=get(hcbar,'xlim'); cbarxtick=get(hcbar,'xtick'); cbarxticklabel=get(hcbar,'xticklabel');
cbarylim=get(hcbar,'ylim'); cbarytick=get(hcbar,'ytick'); cbaryticklabel=get(hcbar,'yticklabel');
newHcbar=axes('Position',cbarpos,'tag','newColorbar');
NC=size(cmap,1);
if isempty(cbarxtick)
   %vertical
   yy=linspace(cbarylim(1),cbarylim(2),NC+1);
   yy=[yy(1:NC); yy(1:NC); yy(2:NC+1); yy(2:NC+1); yy(1:NC)];
   xx=[cbarxlim(1) cbarxlim(2) cbarxlim(2) cbarxlim(1) cbarxlim(1)];
   xx=xx'*ones(1,NC);
else
   %horizontal
   xx=linspace(cbarxlim(1),cbarxlim(2),NC+1);
   xx=[xx(1:NC); xx(1:NC); xx(2:NC+1); xx(2:NC+1); xx(1:NC)];
   yy=[cbarylim(1) cbarylim(2) cbarylim(2) cbarylim(1) cbarylim(1)];
   yy=yy'*ones(1,NC);
end
for jj=1:NC
    hf=fill(xx(:,jj),yy(:,jj),cmap(jj,:)); set(hf,'linestyle','none'); hold on;
end
set(newHcbar,'YAxisLocation',get(hcbar,'YAxisLocation'), ...
             'XAxisLocation',get(hcbar,'XAxisLocation'), ...
             'YScale',get(hcbar,'YScale'), 'XScale',get(hcbar,'XScale'), ...
             'TickDir',get(hcbar,'TickDir'), ...
             'xlim',cbarxlim,'ylim',cbarylim, ...
             'xtick',cbarxtick,'ytick',cbarytick, ...
             'xticklabel',cbarxticklabel,'yticklabel',cbaryticklabel,...
             'fontweight',get(hcbar,'fontweight'), 'fontsize',get(hcbar,'fontsize'),'linewidth',get(hcbar,'linewidth'), ...
             'XDir',get(hcbar,'XDir'),'YDir',get(hcbar,'YDir'), 'XColor',get(hcbar,'XColor'),'YColor',get(hcbar,'YColor'));
set(gcf,'NextPlot',oriNPlot);
set(gcf,'CurrentAxes',oriH); colorbar off;
set(oriH,'position',oriPos)
if nargout>0, newhcb = newHcbar; end

Tuesday, May 10, 2011

png2eps: convert png image to eps with high resolution

#!/bin/bash
# convert a png file into eps file
# Usage:
#           png2eps pngfile.png [-q JPGQuality -r FigResizePercentage]
# e.g.,
#           png2eps aa.png bb.png -q 100 -r 60
# http://scriptdemo.blogspot.com

if [ $# == 0 ]; then
   sed -n '3,6p' png2eps
   exit
fi
JPGQuality='100'
FileResize='80'

nfile=0
while test -n "$1"
do
  case "$1" in
    -q|-quality)
       JPGQuality=$2
       shift
       ;;
    -r|-resize)
       FileResize=$2
       shift
       ;;
    *)
       if [ -e $1 ]; then
          pngfiles[$nfile]=$1
          nfile=`expr $nfile + 1`
       fi
       shift
       ;;
  esac
done
for pngfile in ${pngfiles[*]}
do
  #convert to jpg file first to keep high resolution
  jpgfile=${pngfile%%.*}.jpg
  epsfile=${pngfile%%.*}.eps
  eval "convert -quality ${JPGQuality} -resize ${FileResize}% ${pngfile} ${jpgfile}"
  [ -e ${epsfile} ] && epsfile=tmp_${epsfile}
  eval "convert ${jpgfile} eps3:${epsfile} && rm -f ${jpgfile}"
done
Another solution is to use png2pnm and pnm2ps commands:
#!/bin/bash
# convert a png file into eps file [using pngtopnm, pnmtops]
# Usage:
#           png2eps2 pngfile.png -scale reScale [-o outfile.eps]
# e.g.,
#           png2eps2 aa.png -scale 0.3 -o aa.eps
# http://scriptdemo.blogspot.com

if [ $# == 0 ]; then
   sed -n '3,6p' png2eps2
   exit
fi
reScale='0.3'

nfile=0
while test -n "$1"
do
  case "$1" in
    -s|-r|-scale|-rescale)
       reScale=$2
       shift
       ;;
    -o|-output)
       epsfile=$2
       shift
       ;;
    *)
       if [ -e $1 ]; then
          pngfiles[$nfile]=$1
          nfile=`expr $nfile + 1`
       fi
       shift
       ;;
  esac
done
for pngfile in ${pngfiles[*]}
do
  #convert to jpg file first and resize
  [ ! -n "$epsfile" ] && epsfile=${pngfile%%.*}.eps
  [ $nfile -gt 1 ] && epsfile=${pngfile%%.*}.eps
  [ -e ${epsfile} ] && epsfile=tmp_${epsfile}
  eval "pngtopnm ${pngfile} | pnmtops -noturn -nocenter -scale ${reScale} - >${epsfile}"
  eps2eps -dLanguageLevel=3 ${epsfile} thisTmp${epsfile} && mv thisTmp${epsfile} ${epsfile}
done

ShowCalendar