-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmartdisplay.m
40 lines (34 loc) · 1.24 KB
/
smartdisplay.m
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
function [resstring, sigstring] = smartdisplay(value, err, precise)
% function [resstring, sigstring] = smartdisplay(value, err, precise)
% a function to make a string of values in a format
% value +/- err, using the leading term of the error.
if nargin < 3
precise = 0;
end
% this log10(1.95) permit having more digits if the leading order is 1.
digit = floor(log10(err) - log10(1.95));
if precise
digit = digit - 1;
end
if isinf(digit) % means zero error. special case: follow the num2str rule.
resstring = sprintf(['%s ' char(177) ' 0'], num2str(value));
elseif digit < 0 % floating number, should be most cases
format = sprintf('%%.%df', abs(digit));
resstring = sprintf([format ' ' char(177) ' ' format], value, err);
else
format = '%.0f';
% round the value below significant figure
err2 = round(err .* 10^(-digit)) * 10^(digit);
resstring = sprintf([format ' ' char(177) ' ' format], value, err2);
end
if nargout > 1
sigval = sigmaToPval(value, err, 0);
if sigval < 0.0001;
sigstring = 'p < 0.0001';
else
lord = abs(floor(log10(sigval)));
lord = max(lord, 2); % ensure 2 digits.
format = sprintf('%%.%df', lord);
sigstring = sprintf(['p = ' format], sigval);
end
end