220 likes | 359 Vues
Output. Presenting results to the USER in a professional manner semicolon, disp(), fprintf() Placeholders Special characters Format-modifiers. Where we left off…. % Collect inputs from the user Base = input( ‘Enter base of triangle: ’ ); Height = input ( ‘Enter height of triangle: ’ );
E N D
Output Presenting results to the USER in a professional manner semicolon, disp(), fprintf() Placeholders Special characters Format-modifiers
Where we left off… % Collect inputs from the user Base = input(‘Enter base of triangle: ’); Height = input(‘Enter height of triangle: ’); Units = input(‘Enter unit system chosen: ’, ‘s’); % Compute the area of the triangle Area_tri = 0.5 * Base * Height; % Display the answer on the screen % ??? How is the output displayed?
Several Approaches to Display Results • There are multiple ways to display the value of a variable • Use the semicolon appropriately • use the disp() built-in function • use the fprintf() built-in function • Each is used for specific reasons • Debugging – finding problems with the code • Simple programs, simple results (the programmer’s use) • Formatted (“professional”) output
Several Approaches to Display Results For example: • Use the semicolon appropriately • use the disp() built-in function • use the fprintf() built-in function
Approach #1 – the semi-colon % Collect inputs from the user Base = input(‘Enter base of triangle: ’); Height = input(‘Enter height of triangle: ’); Units = input(‘Enter unit system chosen: ’, ‘s’); % Compute the area of the triangle Area_tri = 0.5 * Base * Height • Not very professional (nothing indicates where and what the output is). The number of decimal places cannot be controlled, and it generally defaults to 4 in MATLAB.
Approach #2 – disp() % Collect inputs from the user Base = input(‘Enter base of triangle: ’); Height = input(‘Enter height of triangle: ’); Units = input(‘Enter unit system chosen: ’, ‘s’); % Compute the area of the triangle Area_tri = 0.5 * Base * Height; % Display the answer on the screen disp(Area_tri); WORSE!!! The number of decimal places cannot be controlled, and it generally defaults to 4 in MATLAB.
Placeholders – why and how • What information is this output missing? • What would make it look professional? Enter base of triangle: 3.2 Enter height of triangle: 4 Enter unit system chosen: cm 6.4000
Approach #3 - Using fprintf() • fprintf(...)% is the built-in function 2. fprintf('format String InSeRtEdhErE!') % The format string allows you to put words and specify a format (UPPER CASE vs. lower case, and punctuation only) • fprintf('format string with placeholders', list of variables to print are inserted here). %If more than one variable is to be printed, each is separated by a comma from the previous one) %Placeholdersallow a specific format to be set (aligned right, and 2 decimal places for example)
Placeholders – why and how • Placeholders are codes used in a format string which let the programmer use values stored in variables (without knowing the actual value) • Why are placeholders needed? • Suppose a variable, result, holds a value. Let’s further suppose it holds a float. • How does the program print the value? Remember – the programmer may not know what value is in the variable. It may be computed during the running of the program.
Using placeholders The fprintf() function should printthe value stored in the variableresult. fprintf('The value in result is %f meters.', result); “placeholder” (part of format string) Result: >> fprintf('The value in result is %f meters.', result); The value in result is 33.651243 meters.>> 6 decimal places by default.
Vocabulary • Most common placeholders • Integer %d • Floats %f • Strings %s • A single letter %c fprintf('The value in result is %f meters.', result); “placeholder” (part of format string) variable to be printed “function call” ‘format string’
Printing multiple variables • When more than one variable must be printed to the screen, match each variable with its placeholder, and place the list of variables in order of the placeholders. • Example age = input('Your age? '); %ask for age name = input('Your name? ', 's'); %ask for name fprintf('%s is %d years old. ', name , age); %display • Sample run: Your age? 47 Your name? Fred Fred is 47 years old.>>
Special Characters • Escape sequences can also be used within the format string: \n - this will create a new line when printing the string \t - tab (tabs the text to the right) '' - this will place one apostrophe in the final sentence displayed %% - this will display a single % sign within the final sentence • Example of all three: >> fprintf('%s''s age:\t\t%d years old\n\n', name, age); Fred's age: 47 years old >>
Format Modifiers • Once the base placeholder is ready,modifiers further change how the values are displayed. • Complete Format Modifier form: %-7.2f Number of decimal places Left-justify the value TOTALwidth to occupy
Format Modifiers • To display a floating point value to 3 decimal places: fprintf('The value of pi: %-7.3f.', pi); • Output: The value of pi: 3.142__ The value of pi: 3. 1 4 2 . Underscores indicate whitespace – they will not actually show in the output. There are 7 spaces occupied
Format Modifiers • When debugging, it can be helpful to “delimit” the output (using dashes and > < symbols) – this lets you see where the “white space” is: fprintf('The value is:\t-->%9.3f<--\n\n', pi); • Output: The value is: --> 3.142<-- >> The delimiters make it easy to notice the white space: spaces, tabs, newlines
Format Modifiers • Normally we don’t use the decimal place portion of format modifiers for strings, and it doesn’t work at all for integers – but the other portions still work! • Example name = ‘Fred’; age = 47; fprintf(‘%-6s is %4d years old!\n’, name, age); • Output: Fred is 47 years old! Note the spaces
Common Mistakes: Do not print a value Can this be the solution? fprintf('The value in result is 23.4\n'); Result: >> fprintf('The value in result is 23.4\n'); The value in result is 23.4 >>
Common Mistakes: Do not print the variable name Can we just say this? fprintf('The value in result is: result\n'); Result: >> fprintf('The value in result is: result\n'); The value in result is: result >>
Common Mistake: Forgetting the placeholder! How about this? fprintf('The value in result is: ', result); Result: >> fprintf('The value in result is: ', result); The value in result is: >>
Conclusion: Approach #3 – fprintf() % Collect inputs from the user Base = input(‘What is the base in inches? ’); Height = input(‘What is the height in inches? ’); Units = input(‘Enter unit system chosen: ’, ‘s’); % Compute the area of the triangle Area_tri = 0.5 * Base * Height; % Display the answer on the screen fprintf(‘\nWiththis data, the area is %.2f %s^2.\n’, Area_tri, Units); • Best ever! The programmercontrols every detail exactly! The PROGRAMMER decides the number of decimals.
Key Ideas • Display strings to the screen to: • Give an introduction/welcome screen to the software • Give error messages when invalid inputs • Terminate the program nicely • And of course… To display results • Omit the semicolon (debugging purposes) • Use disp() – debugging purposes as well • Use fprintf() – specify a very specific format to display from 0 to an unlimited amount of variables • fprintf(…) requires placeholders, with or without any format modifiers: %d, %f, %s, %c, %-10.2f, %-5s, %2d