0
0
MATLABdata~10 mins

String formatting (sprintf) in MATLAB - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to format the number 10 as a string using sprintf.

MATLAB
str = sprintf([1], 10);
Drag options to blanks, or click blank then click option'
A'%d'
B'%s'
C'10'
D'%f'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '%s' which is for strings, not numbers.
Putting the number directly as a string instead of using a format specifier.
2fill in blank
medium

Complete the code to format the number 3.14159 with two decimal places using sprintf.

MATLAB
str = sprintf([1], 3.14159);
Drag options to blanks, or click blank then click option'
A'%s'
B'%.2f'
C'%d'
D'%e'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '%d' which formats as integer and loses decimals.
Using '%e' which formats in exponential notation.
3fill in blank
hard

Fix the error in the code to correctly format the string with a name and age.

MATLAB
str = sprintf('Name: %s, Age: [1]', 'Alice', 30);
Drag options to blanks, or click blank then click option'
A%d
B%s
C%f
D%c
Attempts:
3 left
💡 Hint
Common Mistakes
Using '%s' which expects a string, causing a mismatch.
Using '%f' which formats floating-point numbers.
4fill in blank
hard

Fill both blanks to format a floating number with 3 decimals and a string.

MATLAB
str = sprintf('Value: [1], Status: [2]', 12.34567, 'OK');
Drag options to blanks, or click blank then click option'
A%.3f
B%d
C%s
D%e
Attempts:
3 left
💡 Hint
Common Mistakes
Using '%d' for the floating number, which truncates decimals.
Using '%f' without precision, showing too many decimals.
5fill in blank
hard

Fill all three blanks to format a string with a name, integer age, and floating-point score with 1 decimal.

MATLAB
str = sprintf('Name: [1], Age: [2], Score: [3]', 'Bob', 25, 88.765);
Drag options to blanks, or click blank then click option'
A%s
B%d
C%.1f
D%f
Attempts:
3 left
💡 Hint
Common Mistakes
Using '%f' without precision for the score.
Using '%s' for the age, causing type mismatch.