How to add leading zeros in MatLab (number formatting)? - matlab

Here is a very specific example
>> S = num2str(12345,'%6.0e')
S =
1e+04
and that's just great since I want only my first digit and an exponential notation. However I also want to add leading zeros to the exponent in order to fill the width, but I cannot quite find the way to get the following...
1e+004
Meanwhile it's very straighforward to pad the significant digits with leading zeros
>> S = num2str(12345,'%06.0e')
S =
01e+04
So is there an appropriate formatting for what I want? Or a trick to accomplish it quickly?

The exponent is always a zero-padded two-digit value. To add, say, two zeros you can use
regexprep(num2str(12345, '%6.0e'), '\+', '\+00')
and achieve
ans =
1e+0004
Edit: To cover negative exponents you may use
regexprep(num2str(0.12345, '%6.0e'), '(\+|\-)', '$100')
to achieve
ans =
1e-0001
And, to cover three-digit exponents
regexprep(num2str(1e-100, '%6.0e'), '(\+|\-)(\d{2,3})$', {'$10$2', '$10$2'})
ans =
1e-0100
regexprep(num2str(1e-10, '%6.0e'), '(\+|\-)(\d{2,3})$', {'$10$2', '$10$2'})
ans =
1e-0010

Well, I think you have to edit, what you say you want is wat you get :D
however, if I understood correctly what you are looking for, this function will help you
function printX(x, digits)
format = sprintf('\t%%.%de', digits - 1);
strcat(inputname(1), ' = ', sprintf(format, x))
end

Related

Specify maximum width of printed value in MATLAB

The field width parameter of sprintf and fprintf (e.g., "n" in "sprintf('%nf',1234)") specifies the minimum width of the field. This means that shorter numbers will be padded but longer numbers will be allowed to extend beyond the specified field width when this is necessary to maintain the specified precision. I am looking for a way to specify the maximum field width such that width supersedes precision.
For example, I would want 1234.56, 1234.56789, and 1.2345 to have the width of 8 characters (including the decimal) and therefore print as 1234.560, 1234.567, and 1.234500, respectively.
Here is a similar question, but without a MATLAB-specific solution
Specifying maximum printf field width for numbers (truncating if necessary)?
I feel like someone has to have encountered this problem before, but I was unable to locate anything relevant. Please provide a link if a similar question exists. Thanks
You can use floor(log10()) to determine how many non floating digits you have:
X = [1234.56, 1234.56789, 1.2345]
n = 8 % number of total digits.
% We create an array of string containing the format:
%[ '%.4f\n'
% '%.4f\n'
% '%.7f\n' ]
form = strcat('%.',num2str(n-1-floor(real(log10(X))).'),'f\n')
% We concatenate this array into one single string and we print the result:
fprintf(reshape(form.',1,[]),X)
Or you can use a for loop (using the same logic):
for x = X
form = strcat('%.',num2str(7-floor(real(log10(x)))),'f\n');
fprintf(form,x)
end
And we obtain:
1234.5600
1234.5679
1.2345000
Edit:
I use floor(log10()) instead of ceil(log10()) so the code won't fail if a number is part of the set 10^n (1,10,100...).
And now this snippet can deal with negative floating number since I only take the real part of the log10 output.
if you don't mind truncating instead of rounding the hidden digits, try
S = num2str( X(:), '%-10.9f'); % creates a left-justified character-array
fprintf( '%s\n', string( S(:,1:7) ) )
(X would need to be checked for values too big.)

Matlab reading numbers with higher precision

I have made this scripts that calculates the frequency of a given dataset, but matlab is not precise enough, is it possible to make matlab read in more accurat numbers and not cut off the numbers? I want it to use 8 digits (0.12345678) instead of 4 (0.1234) that is does now
fid = fopen('forceCoeffs.dat','rt');
A = textscan(fid, '%f%f%f%f%f%f', 'HeaderLines',9,'Collect', 9);
A = A{1};
fclose(fid);
t = A(:,1);
Fs = 1/(A(1,1));
x = A(:,2)
x = detrend(x,0);
xdft = fft(x);
freq = 0:Fs/length(x):Fs/2;
xdft = xdft(1:length(x)/2+1);
plot(freq,abs(xdft));
[~,I] = max(abs(xdft));
fprintf('Maximum occurs at %d Hz.\n',freq(I));
File: https://drive.google.com/file/d/0B9CEsYCSSZUSb1JmcHRkbFdWYUU/view?usp=sharing
Thank you for including the forceCoeffs.dat file as it allowed me to run your code. Here is an explanation of what you are seeing.
First I want to point out that MATLAB is not rounding anything. You can check the data type of A to ensure you have enough precision.
>> class(A)
ans =
double
And since you are reading in the file using %f for each column, MATLAB will use all the bits provided by the double type. Ok, now take a look at the contents of your file. The first column has only 2 decimals of precision at most.
0.05 -7.013874e-09 1.410717e+02 -6.688450e-02 -3.344226e-02 -3.344224e-02
...
349.95 -1.189524e-03 1.381022e+00 -2.523909e-01 -1.273850e-01 -1.250059e-01
350 -1.423947e-03 1.380908e+00 -2.471767e-01 -1.250123e-01 -1.221644e-01
Since no more is needed MATLAB only prints four decimal places when you look at the variable in the variable explorer. Try looking at one of the other columns to see what I am talking about. I commented out the A = A{1} part of your code and looked at the second column. When clicking on the number you see the full precision.
You can use a long type to display 16 digits
To get more than 4 digits precision, you can use
format long
However, to get exactly 8 digits, you need to round it. If your number is a then let use:
format long
round(1e8*a)*1e-8

Extract digits to the right of the decimal point

e.g. x = 12.354
I want to get 354 from x. For this I tried this equation,
y = x - floor(x)
But this generates 0.354 which is not my requirement.
So, how can I do it?
The generic idea as pointed out by #JoachimPileborg looks like this in MATLAB:
x = 12.354;
str = num2str(x);
idx = find(str=='.');
substr = str(idx+1:end);
y = str2num(substr);
A generic solution that should work with all programming languages, is to convert the number to a string, then take the sub-string from after the decimal point (or comma) and convert it to an integer.
An alternative (possibly faster) to m.s. 's answer. Notice the 'shift' due to this approach effectively removing the zeroes to the right of the decimal point.
yfoo = 34.00267400;
yfoo = yfoo- floor(yfoo); % .00267400;
% internal rep of number ignores trailing zeros.
yrab = str2num(strrep(num2str(yfoo),'.','')); %2674

Matlab fprintf to keep significant figures and rightmost zeroes

Let's say I have a random variable a=1.2400, and I want to print it with four significant figures, i.e., 1.240. How would I go about that?
fprintf('%0.4g',a) % drops rightmost zero
fprintf('%0.3f',a) % give too many sig figs if a >= 10
Using '%g' drops the important zeros, and with '%f' I can only specify the number of digits after the decimal, which results in too many significant figures if, say, a=10.04. I'm not too familiar with formatting ,but there has to be a simple method. I haven't found it in my searches.
If the values to be printed are all less than 10000, you can do the following. (Sorry, only tested in octave.)
octave:62> a = 1.24
a = 1.2400
octave:63> sprintf('%.*f\n', 3-floor(log10(abs(a))), a)
ans = 1.240
octave:64> a = 234.56
a = 234.56
octave:65> sprintf('%.*f\n', 3-floor(log10(abs(a))), a)
ans = 234.6
For more about the expression floor(log10(abs(a))), see How can I get the exponent of each number in a np.array?
If you don't mind exponential notation, another alternative is to use '%.3e' to always get the same number of signficant digits:
octave:70> a = 1.24
a = 1.2400
octave:71> sprintf('%.3e\n', a)
ans = 1.240e+00
octave:72> a = 234.56
a = 234.56
octave:73> sprintf('%.3e\n', a)
ans = 2.346e+02
I decided to build on the answer by Warren, and I wrote a function that should work for both small and large numbers alike. Perhaps someone will improve on this, but I am pleased with it.
function str=sigfigstr(a,sigfigs)
numdecimal = floor(log10(abs(a)));
if sigfigs - numdecimal < 0
str=sprintf('%.0f',round(a,sigfigs,'significant'));
else
str=strip(sprintf('%.*f\n', sigfigs-floor(log10(abs(a))), a));
end
Here are a few examples if it in action in Matlab
>> sigfigstr(.000012431634,3)
ans = '0.0000124'
>> sigfigstr(26666,3)
ans = '26700'

Leading zero in Matlab's scientific notation

In Matlab, when printing using e such as fprintf('%10.5e\n', pi/100) one will get the result to be 3.14159e-02. However, what if I want the number to have a leading zero such as 0.314159e-1? How can I manage this with Matlab?
The reason I ask is that I am trying to print to a file which I need to have leading zeros. Otherwise, I would not care.
Thanks.
I don't think there is any clever way to do it:
your_number = pi;
['0.' strrep(sprintf('%10.5e',your_number*10), '.', '')]
>> ans =
0.314159e+01
my solution is pretty crude but this is just to illustrate. You can do it yourself with a small function that will look for the relevant strings in the number, trim it after e, add 0. in the beginning and increse by 1 the exponent at the end, for example:
function b=fooo(a)
b=a;
k1 = strfind(a, '.');
k2 = strfind(a, 'e-');
suffix=num2str(str2num(b(k2+1:k2+3))+1);
b(k2+1:end)=[];
b(k1)=[];
b=['0.' b suffix];
where an input like
ans=fooo(sprintf('%10.5e\n', pi/100))
will yield the answer:
ans =
0.314159e-1