How to Print a Table Using FPrintF in Matlab

Techwalla may earn compensation through affiliate links in this story. Learn more about our affiliate and product review process here.
Design your table before entering, formatting and printing it in Matlab.
Image Credit: Jupiterimages/Photos.com/Getty Images

Printing a table in Matlab doesn't always mean that your table is going to look good in printed form. Use the fprintf function in Matlab to make your table printout look good. Statements you include within fprintf -- starting with the boxSizeTable function -- allow you to add column labels, set table dimensions and format table variables, giving you total control over the appearance and formatting of table information. Analyze table information, decide on labels and, if necessary, draw a sketch to make using fprintf in Matlab easier.

Advertisement

Step 1

Open Matlab and start a new project by selecting "New" from the "Window" tab on the Matlab main menu.

Video of the Day

Step 2

Type "function boxSizeTable" on the first line and identify the purpose of the table by adding a comment such as "% This table will demonstrate printing with fprintf" on the second line.

Advertisement

Step 3

Identify table information by setting column labels and row identifiers, and specifying the contents each cell will contain. For a table that identifies available sizes for shipping boxes, for example, type the following:

label = char('small','medium','large'); width = [5; 5; 10]; height = [5; 8; 15]; depth = [15; 15; 20]; % volume is measured in cubic meters vol = width._height._depth/10000;

Advertisement

Step 4

Start the fprintf function by giving the table a title. Identify the function and the title, then end the statement with the special character -- "\n'" -- that instructs Matlab to immediately process the next line of code:

fprintf('\nShipping Box Sizes\n\n');

Advertisement

Step 5

Add a second fprintf statement to print row identifiers and move to the next line of code:

Advertisement

fprintf('size width height depth volume\n');

Step 6

Type in additional fprintf statements to complete table formatting. In this table, for example, you add one additional statement to tell fprintf to inform readers the table measurements are in centimeters rather than inches:

Advertisement

fprintf(' (cm) (cm) (cm) (m^3)\n');

Step 7

Finish formatting by adding a "for" loop that adds field width spacing, formats text and numbers via conversion codes, and instructs fprintf to continue printing until all the information you specified prints. For example, "8s" instructs fprintf to allow eight character spaces and format the line as a string, while "8d" allows eight character spaces and instructs fprintf to format the line as an integer:

Advertisement

for i=1:length(width) fprintf('%-8s %8d %8d %8d %9.5f\n',... label(i,:),width(i),height(i),depth(i),vol(i))

Step 8

Save and view your results on your screen, or select "Print" from the "File" tab on the main menu and print your results on paper.

Video of the Day

Advertisement

Advertisement

references