Friday, March 13, 2015

[matlab] relocate an object in the figure window

function movePosition(myh,myfrac,opt)
% move an object to a new location relative to current axes
% usage:
%      movePosition(obj-handle,fraction,opt)
%             opt: 'x' ==> x-coordiante is given in fraction of xlim
%                    'y' ==> y-coordiante is given in fraction of ylim
%                  'xy' ==> both x,y-coordiante is given in fraction
%                                (could be a 2-element vector)
% http://scriptdemo.blogspot.ca

if nargin~=3
   help movePosition
   return
end
if numel(myh)>1
   for nh=1:numel(myh)
       movePosition(myh(nh),myfrac,opt);
   end
   return
end
if ~ishandle(myh)
   error('need a valid object handle')
end

oriPos=get(myh,'position');
if isempty(oriPos)
    error('no position attribute is available')
end
if isnumeric(opt)
   oriPos(1)=myfrac;
   oriPos(2)=opt;
else
   switch lower(opt)
    case {'x'}
        cXLim=get(gca,'xlim');
        oriPos(1)=oriPos(1)+myfrac*(cXLim(2)-cXLim(1));
    case {'y'}
        cYLim=get(gca,'ylim');
        oriPos(2)=oriPos(2)+myfrac*(cYLim(2)-cYLim(1));
    case {'xy'}
        if numel(myfrac)==1
            myfrac=[myfrac myfrac];
        end
        cXLim=get(gca,'xlim');
        oriPos(1)=oriPos(1)+myfrac(1)*(cXLim(2)-cXLim(1));
        cYLim=get(gca,'ylim');
        oriPos(2)=oriPos(2)+myfrac(2)*(cYLim(2)-cYLim(1));
    otherwise
        error(['unknown opt: ',opt])
   end
end
set(myh,'position',oriPos);

[matlab] show available tags

function showtags(figH)
% to show all the tags in figure(s)
% usage:
%        showtags([figure_handles])
% e.g.,
%        showtags(gcf)
% http://scriptdemo.blogspot.ca

if nargin==1
   availableTag=unique(get(findobj(figH),'tag'));
   availableTagC=unique(get(findobj(figH),'Tag'));
else
   availableTag=unique(get(findobj(),'tag'));
   availableTagC=unique(get(findobj(),'Tag'));
end
if sum(~cellfun('isempty',availableTag))~=0
    disp('Available tags: ')
    disp(availableTag)
end
if sum(~cellfun('isempty',availableTagC))~=0
    disp('Available Tags: ')
    disp(availableTagC)
end

Wednesday, August 6, 2014

[Bash] sum numbers in an array

#!/bin/bash
# sum all/part the numbers in an array using bc
# usage:
#           mysum number-array [index0 index1]
#           Note: the index starts from 1
# e.g.,
#      myA=(34 23 32.5 33.2 25)
#      mysum "${myA[*]}" ==> 147.7
#      mysum "${myA[*]}" 1 2 ==> 57
#      mysum "${myA[*]}" 2 4 ==> 88.7
#      mysum "12 23 12" ==> 47
#
# http://scriptdemo.blogspot.com

isDebug=0
if [ $# -eq 0 ]; then
   sed -n '3,8p' `which mysum`
   exit
else
  myA=($1)
  [ ${isDebug} -eq 1 ] && echo ${myA[*]}
  [ ${isDebug} -eq 1 ] && echo "narg: $#"
  if [ $# -eq 1 ]; then
     if [ ${isDebug} -eq 1 ]; then
        echo "sum: `echo ${myA[*]} | sed 's/\ /+/g' | bc -l`"
     else
        echo "`echo ${myA[*]} | sed 's/\ /+/g' | bc -l`"
     fi
  elif [ $# -eq 2 ]; then
     ind0=`expr $2 - 1`
     nC=`expr ${#myA[*]} - ${ind0}`
     if [ ${isDebug} -eq 1 ]; then
        echo "sum: `echo ${myA[*]:${ind0}:${nC}} | sed 's/\ /+/g' | bc -l`"
     else
        echo "`echo ${myA[*]:${ind0}:${nC}} | sed 's/\ /+/g' | bc -l`"
     fi
  elif [ $# -eq 3 ]; then
     ind0=`expr $2 - 1`
     nC=`expr $3 - ${ind0}`
     if [ ${isDebug} -eq 1 ]; then
        echo "sum: `echo ${myA[*]:${ind0}:${nC}} | sed 's/\ /+/g' | bc -l`"
     else
        echo "`echo ${myA[*]:${ind0}:${nC}} | sed 's/\ /+/g' | bc -l`"
     fi
  fi

ShowCalendar