File: NameFrequency.m

package info (click to toggle)
psychtoolbox-3 3.0.14.20170103%2Bgit6-g605ff5c.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 103,044 kB
  • ctags: 69,483
  • sloc: ansic: 167,371; cpp: 11,232; objc: 4,708; sh: 1,875; python: 383; php: 344; makefile: 207; java: 113
file content (77 lines) | stat: -rw-r--r-- 1,645 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
function fName=NameFrequency(fValue, numDecimalPlaces)
% fName=NameFrequency(fValue, [numDecimalPlaces])
%
% NameFrequency accepts a frequency (in Hz) and returns a
% string naming that frequency in a more readable form.  For example:
%
%     >> c=Screen('Computer');
%     >> c.hw.cpufreq
% 
%     ans =
% 
%        999999997
% 
%     >> NameFrequency(c.hw.cpufreq)
% 
%     ans =
% 
%     1.00 GHz
% 
%     >> NameFrequency(c.hw.cpufreq,6)
% 
%     ans =
% 
%     999.999997 MHz
% 
%     >>
%
%
% By default NameFrequency displays two digits to the right of the decimal
% point. Specify other numbers of digits by passing the optional
% numDecimalPlaces argument.  
%
% NameFrequency is used by DescribeComputer to name the clock frequency of
% the CPU. For that purpose, it displays frequency to a specified number of
% decimal places, not to a specified precision.  It is therfore a poor
% choice for scientific work, where typcially a fixed precision in digits
% is appropriate.  
%
% see also: NameBytes, DescribeComputer

% HISTORY 
% 12/17/04   awi  Wrote it.

 
if nargin < 2
    numDecimalPlaces=2;
end

fNames={
'Hz',
'KHz',
'MHz',
'GHz',
'THz',
'PHz',
'EHz',
'ZHz',
'YHz'
};

numNames=length(fNames);
foundIndex=0;
for i=1:numNames
    if round(fValue/10^((i-1)*3) * (10^numDecimalPlaces)) / (10^numDecimalPlaces) < 1000.00
        namesIndex=i;
        foundIndex=1;
        break;
    end
end
if ~foundIndex
    error('Failed to find a name for the value');
end
        
unitsName=fNames{namesIndex};
sprintFormat=['%3.' int2str(numDecimalPlaces) 'f'];
fName=[sprintf(sprintFormat, fValue/10^((namesIndex-1)*3)) ' ' unitsName];