Formatted Output fprintf Statement • Syntax: • You can write formatted output (text) to the screen or a file using the fprintf command. fprintf('text and/or format commands', variables) • This output can include values of various variables as well as descriptive text. • The format commands control how the values of the variables are printed (e.g., significant digits) as well as the “look” of the output (e.g., tabs and line feeds). • The text can be printed directly or you can use a copy-and-paste operation to include the information in a report. • The variables can be scalars, vectors, or matrices. • More than one variable may be printed using the same fprintf statement. 1 fprintf Statement Continued fprintf Statement Continued • Example of line control format commands: \t \n 2 • Example, remove the % sign from line 7: tab command - move to the next tab stop. new line or line feed command - move to the next line. fprintf('This is a good test'); • These commands control the “look” and “layout” of most of the output so that it is readable and understandable. • These commands appear INSIDE the single quotes. • Save and run the m.file. • Now, remove the % sign from line 8: fprintf('\n\nThis is\t\ta good\n\n test\n\n'); • Other line control commands are available (see help). • Start a NEW m.file. – Copy and paste the FIRST starter m.file for this lesson into the Editor. – Save it to the H:\ drive, name it as5_3ic.m 3 • Save and run the m.file. Note the 8 line control • What is the same? format commands. • What is different? • How would you use these commands to control the “look and “layout” of the output? 4 • Assume we assigned a value to a scalar variable named time. We would like to print it to the screen. Remove the % sign from lines 11 and 12 : fprintf Statement Continued time = 12; • The new line command ( \n ) is useful in adding blank lines fprintf('Time = ',time); at the top and bottom of the output as well as in between various sections of the output. • Save and run the m.file. Why did it NOT work? • The new line command ( \n ) is also useful in creating tables (i.e., vertical lists of data). • We did not specify where and how to print the value stored in the variable time. • Edit the fprintf command (line 12) to: • The tab command ( \t ) is useful in ensuring that vertical time = 12; columns of data “line up” properly, although I prefer to use blank spaces. fprintf('\n\nTime = %f \n\n', time); • Save and run the m.file. What do the \n ’s do? 5 6 fprintf Statement Continued fprintf Statement Continued • The %f is an output format command. It tells Matlab where (right after the = sign) and how (fixed decimal format) to print the number stored in the variable time. • All output variable format commands must start with the % sign (yes, so does the comment operator, and yes this is confusing). The letter may be different. • The format command can appear anywhere in between the single quotes, it does not have to be at the end of the text. Try (edit line 12 again): • Some common “Conversion Specifiers” are: %e %E %f %g time = 12; fprintf('\n\nTime = %f sec. \n\n', time); • Save and run the m.file. %G 7 Exponential notation (using lowercase e as in 3.1415e+00) Exponential notation (using uppercase E as in 3.1415E+00) Fixed decimal point notation (as in 3.14159) The more compact of %e or %f , that is Matlab chooses. Insignificant zeros do not print. Same as %g, but using an uppercase E. 8 fprintf Statement Continued fprintf Statement Continued • Still using line 11: • We can also control the “width” (the total number of spaces used) and the “precision” (number of decimal places) to print. time = 12; • Remove the % sign from lines 15-19: • The general format is: %<flag><width>.<precision><conversion specifier> fprintf('\nTime = %f sec. \n',time); • The <flag> controls items such as leading zeros, always printing the sign (+ or -), etc. See the book or help file. fprintf('\nTime = %6.3f sec. \n',time); • The <width> is the total number of spaces to use in printing the number (including the decimal point). • The <precision> is the number of decimal places to print. fprintf('\nTime = %12.3e sec. \n',time); fprintf('\nTime = %12.3f sec. \n',time); fprintf('\nTime = %12.3g sec. \n',time); • The <conversion specifier> was discussed on the previous page (i.e., e, E, f, g, or G ) . • Save and run the m.file. 9 • Can we print more than one variable on the same line using the same fprintf statement? Yes!! Remove the % sign from lines 22-26: fprintf Statement Continued • Did you get? t = 12; v = 3.4; fprintf('\n\n'); fprintf('t = %6.3f sec and v = %3.1f ft/sec',t,v); fprintf('\n\n'); Time = 12.000000 sec. Time = 12.000 sec. Time = 10 12.000 sec. • Save and run the m.file. Did you get: Time = 1.200e+001 sec. Time = 12 sec. Do the output formats and the printed output match? t = 12.000 sec and v = 3.4 ft/sec • Notice how the printed output changes when we change the width, precision, and conversion specifier. • Also notice where “padding” (extra spaces) appear. 11 • Note that the 1st and 3rd fprintf statements just add the extra line feeds to give some “extra space.” • Also, ALL format commands and ALL text must between the single quotes and the variable names appear at the end. 12 fprintf Statement for Vectors & Matrices fprintf Statement for Vectors & Matrices fprintf and for loops combined • We can leave the data in vector form and use a for loop to print the table. • Assume we have the following lists of time and associated velocities: • For each “cycle” (iteration) of the for loop we will print a “t” value and a “v” value on the same line. t = [0 1 2 3 4 5 6 7] v = [10 12 14 9 3 -2 -7 -10] • We will only print one “t” and one “v” value per line. • We would like to print a “table” of time and velocity values. • Our table should be two vertical columns and include a title and column headings. • The desired output is shown on the next page. 13 14 • Start with the “basic table” first. t = [0 1 2 3 4 5 6 7]; v = [10 12 14 9 3 -2 -7 -10]; for ii=1:1:length(t) fprintf('\t\t%5.1f\t\t%5.1f\n',t(ii),v(ii)); end Make sure the format control is "wide" Remove % signs enough to print the numbers to the desired p"precision" and print signs ( + or -), etc. Time and Velocity for Our Example Time sec ---0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 Again, let Matlab figure out how many values need to be printed. Velocity ft/sec -------10.0 12.0 14.0 9.0 3.0 -2.0 -7.0 -10.0 • After we initialize the two lists, we will “loop through” all of their values using the index ii. • For each cycle of the loop we will print the corresponding t and v values using the %5.1f format. • We will “tab over” twice, print a t value, “tab over” twice, print a v value, then move down one line before the next cycle starts. 15 16 In-Class Problem • Remove the semicolons and the % signs and run. • Modify our force-plate m.file so that the program creates a table of some of the data: • Save and run the m.file and see what you get. – Will include the time = 0 values of Fx, Fy and Fz. • Now we need to add the title and column headers. The fprintf statements must be BEFORE the for loop. – Will also include the the values of Fx, Fy and Fz at each 0.1 sec. – Will also have an “appropriate header” that includes a Title and Column Headers with Units. • On your own, enter the following lines before the for loop, save the m.file and re-run. • The desired output is shown on the next page fprintf('\n\n\t\tTime and Velocity for Our Example\n\n') fprintf('\t\t Time\t Velocity\n') fprintf('\t\t sec \t ft/sec \n') fprintf('\t\t ----\t --------\n') • Everyone should have a completed m.file by the end of the period. • The completed m.file will include proper documentation. Try different combinations of tabs and spaces and see what you get. 17 Force Plate Data for Subject 3 - Jump 2 Time sec ---0.000 0.100 0.200 0.300 0.400 . . . 1.900 2.000 Fx N ---2 2 8 128 70 . . . 23 26 Fy N ---1 1 12 206 157 . . . 1 0 18 Our Plan Fz N ---1 1 61 943 979 . . . 329 343 19 20