Challenge - 5 Problems
MATLAB String Concatenation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this MATLAB code?
Consider the following MATLAB code that concatenates strings using square brackets. What will be printed?
MATLAB
str1 = 'Hello'; str2 = 'World'; result = [str1, ' ', str2]; disp(result);
Attempts:
2 left
💡 Hint
Remember that square brackets concatenate strings and spaces are literal characters.
✗ Incorrect
In MATLAB, using square brackets concatenates strings exactly as written. Adding ' ' inserts a space between the two words.
❓ Predict Output
intermediate2:00remaining
What does this code output when concatenating numeric and string data?
Look at this MATLAB code that tries to concatenate a number and a string. What will it display?
MATLAB
num = 123; str = ' apples'; result = [num, str]; disp(result);
Attempts:
2 left
💡 Hint
In MATLAB, numeric and character arrays cannot be concatenated directly using square brackets.
✗ Incorrect
MATLAB requires numeric values to be converted to strings before concatenation. Direct concatenation causes a dimension mismatch error.
🧠 Conceptual
advanced2:00remaining
How to concatenate strings with newline characters in MATLAB?
Which option correctly concatenates two strings with a newline between them so that the output shows on two lines?
Attempts:
2 left
💡 Hint
MATLAB has a built-in function for newline characters.
✗ Incorrect
The 'newline' function returns the platform-specific newline character. Using it inside brackets concatenates strings with a newline.
🔧 Debug
advanced2:00remaining
Identify the error in this string concatenation code
This MATLAB code tries to concatenate strings but causes an error. What is the cause?
MATLAB
str1 = 'Data'; str2 = "Science"; result = [str1, str2]; disp(result);
Attempts:
2 left
💡 Hint
MATLAB treats single-quoted and double-quoted strings differently.
✗ Incorrect
Single quotes create character arrays, double quotes create string arrays. They cannot be concatenated directly using [].
❓ Predict Output
expert2:00remaining
What is the output of this complex string concatenation with cell arrays?
Given the following MATLAB code, what will be the output displayed?
MATLAB
C = {'Hello', 'World'};
result = [C{1}, ' ', C{2}];
disp(result);Attempts:
2 left
💡 Hint
Use curly braces to extract content from cell arrays before concatenation.
✗ Incorrect
Using C{1} and C{2} extracts the strings from the cell array. Concatenating with a space produces 'Hello World'.