22
Presenting results to the USER in a professional manner 1.semicolon, disp(), fprintf() 2.Placeholders 3.Special characters 4.Format-modifiers Output 1

Presenting results to the USER in a professional manner 1. semicolon, disp(), fprintf() 2. Placeholders 3. Special characters 4. Format-modifiers Output

Embed Size (px)

Citation preview

Page 1: Presenting results to the USER in a professional manner 1. semicolon, disp(), fprintf() 2. Placeholders 3. Special characters 4. Format-modifiers Output

Presenting results to the USER in a professional manner

1.semicolon, disp(), fprintf()

2.Placeholders

3.Special characters

4.Format-modifiers

Output

1

Page 2: Presenting results to the USER in a professional manner 1. semicolon, disp(), fprintf() 2. Placeholders 3. Special characters 4. Format-modifiers Output

% Collect inputs from the userbase_cm = input(‘What is the base in cm? ’);height_cm = input(‘What is the height in cm? ’);

% Compute the area of the trianglearea_cm2 = 0.5 * base_cm * height_cm;

% Display the answer on the screen% ??? How is the output displayed?

2

Page 3: Presenting results to the USER in a professional manner 1. semicolon, disp(), fprintf() 2. Placeholders 3. Special characters 4. Format-modifiers Output

Display with a specific format

There are multiple ways to display the value of a variable

1. Use the semicolon appropriately

2. use the disp() built-in function

3. use the fprintf() built-in function

Each is used for specific reasons1. Debugging – finding problems with the code

2. Simple programs, simple results (the programmer’s use)

3. Formatted (“pretty”) output

3

Page 4: Presenting results to the USER in a professional manner 1. semicolon, disp(), fprintf() 2. Placeholders 3. Special characters 4. Format-modifiers Output

In a short example, what are the differences?

<enter>

<enter>

<enter>

4

<enter>

Page 5: Presenting results to the USER in a professional manner 1. semicolon, disp(), fprintf() 2. Placeholders 3. Special characters 4. Format-modifiers Output

% Collect inputs from the userbase_cm = input(‘What is the base in cm? ’);height_cm = input(‘What is the height in cm? ’);

% Compute/DISPLAY the area of the trianglearea_cm2 = 0.5 * base_cm * height_cm

Not very pretty (nothing indicates where and what the output is)What is the base in cm? 3.2What is the height in cm? 4

area_cm2 =

6.4000

The number of decimal places cannot be controlled, and it generally defaults to 4 in MATLAB.

5

Page 6: Presenting results to the USER in a professional manner 1. semicolon, disp(), fprintf() 2. Placeholders 3. Special characters 4. Format-modifiers Output

% Collect inputs from the userbase_cm = input(‘What is the base in cm? ’);height_cm = input(‘What is the height in cm? ’);

% Compute the area of the trianglearea_cm2 = 0.5 * base_cm * height_cm;

% Display the answer on the screen% ??? How is the output displayed?disp(area_cm2);

Not very pretty (nothing indicates where and what the output is)What is the base in cm? 3.2What is the height in cm? 4 6.4000

The number of decimal places cannot be controlled using disp() either, and it defaults to 4 as well.

6

Page 7: Presenting results to the USER in a professional manner 1. semicolon, disp(), fprintf() 2. Placeholders 3. Special characters 4. Format-modifiers Output

Using fprintf()

1. fprintf(...) % is the built-in function

2. fprintf(‘format String InSeRtEd hErE!’)

% The format string allows you to put words and specify a format (UPPER CASE vs. Lower case, and punctuation only)

3. 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)

% Placeholders allow a specific format to be set (aligned right, and 2 decimal places for example)

7

Page 8: Presenting results to the USER in a professional manner 1. semicolon, disp(), fprintf() 2. Placeholders 3. Special characters 4. Format-modifiers Output

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.

8

Page 9: Presenting results to the USER in a professional manner 1. semicolon, disp(), fprintf() 2. Placeholders 3. Special characters 4. Format-modifiers Output

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>>

9

Page 10: Presenting results to the USER in a professional manner 1. semicolon, disp(), fprintf() 2. Placeholders 3. Special characters 4. Format-modifiers Output

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>>

10

Page 11: Presenting results to the USER in a professional manner 1. semicolon, disp(), fprintf() 2. Placeholders 3. Special characters 4. Format-modifiers Output

Do not forget the placeholder

How about this?fprintf('The value in result is: ', result);

Nope:>> fprintf('The value in result is: ', result);The value in result is: >>

11

Page 12: Presenting results to the USER in a professional manner 1. semicolon, disp(), fprintf() 2. Placeholders 3. Special characters 4. Format-modifiers Output

Using placeholders

The fprintf() function should print the value stored in the variable result.

Placeholders to the rescue!

Result:>> fprintf(‘The value in result is %f meters.’, result);The value in result is 33.651243 meters.>>

12

fprintf(‘The value in result is %f meters.’, result);

“placeholder”(part of format string)

Page 13: Presenting results to the USER in a professional manner 1. semicolon, disp(), fprintf() 2. Placeholders 3. Special characters 4. Format-modifiers Output

Using placeholders

The fprintf() function should print the value stored in the variable result.

Placeholders to the rescue!

Result:>> fprintf(‘The value in result is %f meters.’, result);The value in result is 33.651243 meters.>>

13

fprintf(‘The value in result is %f meters.’, result);

DIFFERENT THAN ; OR DISP… 6 decimal places by default.

Page 14: Presenting results to the USER in a professional manner 1. semicolon, disp(), fprintf() 2. Placeholders 3. Special characters 4. Format-modifiers Output

Stop for vocabulary

Just a quick recall about the vocabulary.

fprintf(‘The value in result is %f meters.’, result);

“function call”

‘format string’

“placeholder”(part of format string)

variable to be printed

14

Page 15: Presenting results to the USER in a professional manner 1. semicolon, disp(), fprintf() 2. Placeholders 3. Special characters 4. Format-modifiers Output

Most common placeholders

Each data-type has a placeholder Integer %d Floats %f Strings %s A single letter %c

15

Page 16: Presenting results to the USER in a professional manner 1. semicolon, disp(), fprintf() 2. Placeholders 3. Special characters 4. Format-modifiers Output

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.

Exampleage = input(‘Your age? ’); %ask for agename = input(‘Your name? ’, ‘s’); %ask for namefprintf(‘%s is %d years old.’, name , age); %display

Sample run:Your age? 47Your name? FredFred is 47 years old.>>

16

Page 17: Presenting results to the USER in a professional manner 1. semicolon, disp(), fprintf() 2. Placeholders 3. Special characters 4. Format-modifiers Output

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

Example of all three:>> fprintf('%s''s age:\t\t%d years old\n\n', name, age);

Fred's age: 47 years old

>>

17

Page 18: Presenting results to the USER in a professional manner 1. semicolon, disp(), fprintf() 2. Placeholders 3. Special characters 4. Format-modifiers Output

Format Modifiers (1/4)

Once the base placeholder is ready, modifiers further change how the values are displayed.

Complete Format Modifier form:

%-7.2f

Left-justify the value

TOTAL width to occupy

Nb. of decimal places

18

Page 19: Presenting results to the USER in a professional manner 1. semicolon, disp(), fprintf() 2. Placeholders 3. Special characters 4. Format-modifiers Output

Format Modifiers (2/4)

To display a floating point value to 3 decimal places:

fprintf(‘The value of pi: %-7.3f.’, pi);

Output:

The value of pi: 3 . 1 4 2 _ _

The value of pi: 3 . 1 4 2 .

Underscores indicate whitespace – they will not actually show in the output. There are 7 spaces occupied

19

Page 20: Presenting results to the USER in a professional manner 1. semicolon, disp(), fprintf() 2. Placeholders 3. Special characters 4. Format-modifiers Output

Format Modifiers (3/4) 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

20

Page 21: Presenting results to the USER in a professional manner 1. semicolon, disp(), fprintf() 2. Placeholders 3. Special characters 4. Format-modifiers Output

Format Modifiers (4/4)

Obviously, the decimal place portion of format modifiers will not apply to strings and integers – but the others do!

Examplename = ‘Fred’;

age = 47;

fprintf(‘%-6s is %4d years old!\n’, name, age);

Output:Fred is 47 years old!

Note the spaces21

Page 22: Presenting results to the USER in a professional manner 1. semicolon, disp(), fprintf() 2. Placeholders 3. Special characters 4. Format-modifiers Output

Wrapping Up

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 22