CSE 1010 - MATLAB - User-Controlled Input and Output - fprintf

In summary, to display the second column in scientific notation with two decimal places and one digit before the decimal point, use the '%3.2e' format specifier within the fprintf statement.
  • #1
GreenPrint
1,196
0
SOLVED

Homework Statement


Very small dimensions-those on the atomic scale-are often measured in angstroms. An angstrom is represented by the symbol ? and corresponds to a length of 10^-10 meter. Create an inches-to-angstroms conversion table as follows for values of inches from 1 to 10:

Use disp to create a title and column heading

Use fprintf to display the numerical information.

Because the length represented in angstroms is so big, represent your result in scientific notation, showing two values after the decimal point. This corresponds to three significant figures (one before and two after the decimal point).

Homework Equations


The Attempt at a Solution


I can't get MATLAB to corporate with me. I thought the series of commands below would be sufficient and produce the results that I was looking for to solve this problem but I got an answer that I wasn't expecting.
==========
format short e
a=1:10;
b=a.*2.54*10^8;
disp('inches to angstroms')
disp('inches angstroms')
table=[a;b];
fprintf('%3.2f %3.2f\n',table)
==========
This is what is returned by MATLAB after inputing these commands
==========
inches to angstroms
inches angstroms
1.00 254000000.00
2.00 508000000.00
3.00 762000000.00
4.00 1016000000.00
5.00 1270000000.00
6.00 1524000000.00
7.00 1778000000.00
8.00 2032000000.00
9.00 2286000000.00
10.00 2540000000.00
==========
is there anyway I can get MATLAB to display the second column in scientific notation with 2 decimal places and one digit before the decimal point?

Thank you very much!
 
Last edited:
Physics news on Phys.org
  • #2
SOLUTIONThe solution to this problem is to use the '%3.2e' format specifier within the fprintf statement. This will display the second column (the angstroms) in scientific notation with two decimal places and one digit before the decimal point: format short e a=1:10; b=a.*2.54*10^8; disp('inches to angstroms') disp('inches angstroms') table=[a;b]; fprintf('%3.2f %3.2e\n',table)This will produce the following output: inches to angstromsinches angstroms1.00 2.54e+0082.00 5.08e+0083.00 7.62e+0084.00 1.02e+0095.00 1.27e+0096.00 1.52e+0097.00 1.78e+0098.00 2.03e+0099.00 2.29e+00910.00 2.54e+009
 

What is CSE 1010?

CSE 1010 is an introductory course in computer science and engineering that focuses on teaching students how to use MATLAB, a popular programming language, for scientific and engineering applications.

What is the purpose of user-controlled input and output?

User-controlled input and output in MATLAB allows the user to interact with the program by entering input values and receiving output results. This allows for more dynamic and customized programs.

What is fprintf in MATLAB?

fprintf is a function in MATLAB that allows for formatted output to the Command Window or a file. It is commonly used to display user-friendly messages and results.

How do you use fprintf in MATLAB?

To use fprintf in MATLAB, you first need to open a file or the Command Window. Then, you can use the function with the following syntax: fprintf(fileID,format,A). "fileID" is the file identifier or the Command Window, "format" specifies the format of the output, and "A" is the data to be displayed.

What are some common formatting options for fprintf?

Some common formatting options for fprintf include %s for strings, %d for integers, %f for floating-point numbers, and %c for characters. You can also specify the number of decimal places, the alignment, and the width of the output. Refer to the MATLAB documentation for a complete list of formatting options.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
6K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
Back
Top