I'm wanting to change my formatting to scientific and output values in a table

39 views (last 30 days)
An inches-to-angstroms conversion table for values of inches 1-15
%One inch is 254000000 angstroms
%Defined a vector of inch values
Inches=1:15;
%converts inches to angstroms
Angstroms=Inches.*254000000;
table=[Inches,Angstroms];
%Creates table title
disp('Inch to Angstrom Conversion Table')
% Creates colum headings
disp(' Inches Angstrom')
%Display the table
fprintf('%4.0f %7.2f \n',table)
This is what is outputed currently
Inch to Angstrom Conversion Table
Inches Angstrom
1 2.00
3 4.00
5 6.00
7 8.00
9 10.00
11 12.00
13 14.00
15 254000000.00
508000000 762000000.00
1016000000 1270000000.00
1524000000 1778000000.00
2032000000 2286000000.00
2540000000 2794000000.00
3048000000 3302000000.00
3556000000 3810000000.00
I'm wanting to represent my result in scientific notation, showing two values after the decimal point meaning three significant figures.

Accepted Answer

dpb
dpb on 14 Mar 2019
Your table isn't what you think it is...study the output carefully and the orientation of the data in the table...
HINT: You need two rows to print as you've done above irrespective of the format and get the output in the right order; or, to make a table that you can look at as you expect in the Matlab command window you'll want to columns. If you do that, then to print in the same displayed orientation/order you'll need to transpose the array because Matlab stores data in column-major order and fprintf outputs it in memory-storage order.
For the specific Q?
fmt=['%5d %10.2E\n'];
fprintf(fmt,table.')
>> fprintf(fmt,table.')
1 2.54E+08
2 5.08E+08
3 7.62E+08
4 1.02E+09
5 1.27E+09
6 1.52E+09
7 1.78E+09
8 2.03E+09
9 2.29E+09
10 2.54E+09
11 2.79E+09
12 3.05E+09
13 3.30E+09
14 3.56E+09
15 3.81E+09
>>
after I took care of the other memory problem...

More Answers (0)

Categories

Find more on Encryption / Cryptography in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!