Formatting Numeric Output in Matlab The methods described here are covered in Chapter 7 of your text book (MATLAB for Engineers, Holly Moore). Prof. Ratcliffe’s sections are REQUIRED to use formatted output from the very beginning! So use this handout to help. Then when you get to Chapter 7 you will find it very easy! How do you get Matlab to print out the results of a calculation? Matlab has several different methods. For example, if you run the following script: C=98 R=(C+273.15)*9/5 Matlab’s command window will give: C= 98 R= 668.0700 If you happen to know the expected output, or by looking at the code you can guess, then you will recognize that C is a temperature in degrees Celsius, which is converted to a temperature in degrees Rankine. Wouldn’t it be better if your Matlab output had a CAPTION and engineering UNITS? Here’s how we can modify the previous code to give a nicely formatted output. We add semicolons at the end of each line, and add an fprintf statement to print out the results. C=98; R=(C+273.15)*9/5; fprintf('%.1f degrees Celsius converts to %.1f degrees Rankine\n',C,R); Here’s the resulting output – much nicer! 98.0 degrees Celsius converts to 668.1 degrees Rankine Matlab’s fprintf function is very powerful. In this handout we introduce some of the more useful methods. You can search online and Matlab help files for more in-depth use. FORMATTING A SINGLE NUMBER For now, let’s not worry about the caption or engineering units. Let us concentrate on the number. Try typing the following into the Matlab command window, and see how Matlab outputs the result. This is one of Matlab’s default formats. It’s functional, but not very nice. >> x=23.6 x= 23.6000 The simplest fprintf command has two arguments. The first instructs Matlab to use a specific format, and the second is the variable we want to print. There are several different format commands available in Matlab. For now we will only consider two of them. %f %e says use a FIXED POINT format says use EXPONENTIAL format Try these two fprintf examples. Notice that we have included a \n at the end of each format string. For now, just do it. We’ll explain its importance later! gives fprintf('%f \n', x); 23.600000 and gives fprintf('%e \n', x); 2.360000e+01 OK, so the output doesn’t look much better than the Matlab default method (no semicolon). But things get better! Try these two fprintf examples. They both use the fixed point output, but this time we control the number of decimal places. gives fprintf('%.3f \n', x); 23.600 and gives fprintf('%.1f \n', x); 23.6 We can even display integers by telling fprintf to use zero decimal places. Do you think fprintf will print 23.6 truncated to 23 or rounded to 24? Try it. fprintf('%.0f \n', x); Now let’s add a caption. We can add this in the first argument. Basically, fprintf will just print out any text unless it sees the format command (%f or %e). It replaces the %e or %f with the number that is the second argument. gives fprintf('The value of x is %.1f \n', x); The value of x is 23.6 Now let’s add some engineering units! gives fprintf('The value of x is %.1f inches \n', x); The value of x is 23.6 inches FORMATTING SEVERAL NUMBERS In the temperature conversion example above we need to print out two numbers from variables C and R. We do this by adding extra %e or %f format commands. Here’s an example: fprintf('%.1f degrees Celsius converts to %.2e degrees Rankine \n', C, R); gives 98.0 degrees Celsius converts to 6.68e+02 degrees Rankine FORMATTING AN ARRAY OF NUMBERS Very often in Matlab you will have an array (vector, matrix, list) of numbers. How can you print them all out? Try this Matlab code: Gives >> C=25:28 C= 25 26 27 28 We can get a nice format using a single fprintf command: fprintf('Temperature %.1f degrees Celsius \n', C); gives Temperature 25.0 degrees Celsius Temperature 26.0 degrees Celsius Temperature 27.0 degrees Celsius Temperature 28.0 degrees Celsius fprintf keeps re-using the format string until it has printed out all the numbers in array C. See what happens if you do not have the \n at the end of the format string: fprintf('Temperature %.1f degrees Celsius', C); gives Temperature 25.0 degrees CelsiusTemperature 26.0 degrees CelsiusTemperature 27.0 degrees CelsiusTemperature 28.0 degrees Celsius The \n tells Matlab to start a new line. Without the \n, fprintf just keeps on printing across the page. FORMATTING TWO ARRAYS OF NUMBERS Often you will want to print out two arrays (or more) of numbers. For example, we might want a table of degrees Celsius and their respective degrees Rankine. The problem we hit is that fprintf works left-to-right through the variables, re-using the format string as often as it needs. Also, if any of the variables is a full matrix, fprintf works down each column, one at a time. For example, we have a single matrix as follows: s=[1 2 3; 4 5 6] s= 1 2 3 4 5 6 The fprintf command works column-wise. So fprintf('%.1f \n', s); gives 1.0 4.0 2.0 5.0 3.0 6.0 It is important to remember that fprintf works column-wise though each variable, and then from left-to right through the variable list. So how do we get Matlab to print out two arrays of numbers, and format them to look like a table? The trick is to concatenate (combine, join together) the two separate arrays into a single matrix. We must also make sure that the pairs of numbers (e.g., each Celsius temperature and its respective Rankine temperature) follow each other in column-wise order. We have: And C= 25 26 27 28 R= 536.6700 538.4700 540.2700 542.0700 Matlab’s concatenate can be achieved thus: >> [C ; R] ans = 25.0000 26.0000 27.0000 28.0000 536.6700 538.4700 540.2700 542.0700 So now we are ready to print the two arrays of numbers in table format. gives fprintf('%.1f %.2f \n', [C ; R]); 98.0 99.0 100.0 668.07 669.87 671.67 101.0 673.47 Did you notice the slight “bump” in the table at 100 degrees Celsius? This is because 99.0 takes up less space than 100.0. We can make the table look better by changing the fprintf format string. %f says print a fixed point format number using a default format %.2f says print the number with 2 decimal places. %5.2f says print 2 decimal places, but ADD SPACES at the beginning so that the total width of the output is at least 5 places. Let’s try it. We will also add an extra fprintf to create column headers: gives fprintf('Degrees Celsius Degrees Rankine \n'); fprintf('%10.1f %18.2f \n', [C ; R]); Degrees Celsius 98.0 99.0 100.0 101.0 Degrees Rankine 668.07 669.87 671.67 673.47 Try changing the numbers in the %10.1f and see how the table changes width and vertical alignment. Now try changing the fixed point format to exponential format, and set the width. Hint: %10.2e WARNING The previous section that printed a table of the two numbers assumed that both arrays (C and R) were row vectors. That is, they had one row and several columns. Often in MATLAB you will have column vectors. How does this change things? How can you tell if your list (vector) is a row or column? Look in the workspace. If your variable has a value shown something like 1x200, you have a ROW vector If your variable has a value shown something like 200x1, you have a COLUMN vector Alternatively, in the command window, type the variable name and see if it prints in a column or row PRINTING TWO ROW VECTORS IN A TABULAR FORMAT If you need to print two row vectors, use the method in the example above for Celsius and Rankine: fprintf('%10.1f %18.2f \n', [C ; R]); PRINTING TWO COLUMN VECTORS IN A TABULAR FORMAT If you have C and R in COLUMNS: >> C C= 25 26 27 28 >> R R= 536.6700 538.4700 540.2700 542.0700 You need to modify the concatenation method as below. The ‘ after [C R] makes Matlab transpose the matrices (swap rows and columns), giving a matrix ready for printing. fprintf('%10.1f %18.2f \n', [C R]’); CAVEAT As with anything in Matlab, you can often achieve the same result using many different methods. We strongly advise you NOT to use Matlab’s disp function. It may seem easier to use (it is!), but it is less flexible. The fprintf function gives you the versatility you need for all your engineering work.