function outStr=formatStr(inStr,nLen,opt) % pad the input string to specified length with blank space % usage: % outStr=formatStr(inStr,expected-Length [,align-option]) % if align-option == 0 'right-align' [ by default ] % == 1 'center-align' % == 2 'left-align' % history: % Feb 24 2012: first coded for myShowNc % http://scriptdemo.blogspot.com if nargin==1 nLen=0; elseif nargin==2 opt=0; elseif nargin~=3 help formatStr; return end oriLen=length(inStr); if (oriLen>=nLen) outStr=inStr; else switch (opt) case {0} nSpace=nLen-oriLen; outStr=[repmat(' ',1,nSpace),inStr]; case {1} nSpace=ceil(0.5*(nLen-oriLen)); outStr=[repmat(' ',1,nSpace),inStr]; nSpace=nLen-oriLen-nSpace; if (nSpace>0) outStr=[outStr,repmat(' ',1,nSpace)]; end case {2} nSpace=nLen-oriLen; outStr=[inStr,repmat(' ',1,nSpace)]; otherwise disp('Unknown option'); return end end |
Saturday, February 25, 2012
[Matlab] format a string to a specific length
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment