1
0

dicomrt_mmdigit.m 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. function y=dicomrt_mmdigit(x,n,b,t)
  2. % dicomrt_mmdigit(x,n,b,t)
  3. %
  4. % Round values to given significant digits
  5. %
  6. % x is the array to be rounded
  7. % n is the number of significant places
  8. % b is the base (b=10 default)
  9. % t is the type of algorithm to use to round (t='round' default)
  10. % permitted types are also: 'fix', 'ceil' and 'round'
  11. %
  12. % If x is immaginary real and immaginary parts are rounded separately
  13. %
  14. % From "Mastering MATLAB 6" Duane Hanselman and Bruce Littlefield,
  15. % Prentice Hall, 2001 ISBN 0-13-019468-9
  16. %
  17. % Copyright (C) 2002 Emiliano Spezi (emiliano.spezi@physics.org)
  18. if nargin<2
  19. error('dicomrt_mmdigit: Not enough input arguments')
  20. elseif nargin==2
  21. b=10;
  22. t='round';
  23. elseif nargin==3
  24. t='round';
  25. end
  26. n=round(abs(n(1)));
  27. if isempty(b),b=10;
  28. else b=round(abs(b(1)));
  29. end
  30. if isreal(x)
  31. y=abs(x)+(x==0);
  32. e=floor(log(y)./log(b)+1);
  33. p=repmat(b,size(x)).^(n-e);
  34. if strncmpi(t,'round',1)
  35. y=round(p.*x)./p;
  36. elseif strncmpi(t,'fix',1)
  37. y=fix(p.*x)./p;
  38. elseif strncmpi(t,'ceil',1)
  39. y=ceil(p.*x)./p;
  40. elseif strncmpi(t,'floor',1)
  41. y=floor(p.*x)./p;
  42. else
  43. error('dicomrt_mmdigit: Unknown rounding request');
  44. end
  45. else % complex input
  46. y=complex(mmdigit(real(x),n,b,t),mmdigit(imag(x),n,b,t));
  47. end