📖 Scenario: You are creating a simple program to combine parts of a person's full name from separate pieces.
🎯 Goal: Build a MATLAB script that concatenates first name, middle name, and last name into a full name string.
📋 What You'll Learn
Create variables for first name, middle name, and last name with exact given values
Create a variable for a space character to use as separator
Concatenate the three name parts with spaces in between using string concatenation
Display the full concatenated name using disp
💡 Why This Matters
🌍 Real World
Combining parts of names or other text pieces is common in creating user-friendly displays, reports, or documents.
💼 Career
String concatenation is a basic skill useful in data processing, user interface design, and report generation in many programming jobs.
Progress0 / 4 steps
1
Create name parts variables
Create three string variables called firstName, middleName, and lastName with these exact values: 'John', 'Fitzgerald', and 'Kennedy' respectively.
MATLAB
Hint
Use single quotes to create strings in MATLAB, for example: firstName = 'John';
2
Create space separator variable
Create a variable called space and assign it the string value of a single space character ' '.
MATLAB
Hint
Assign a single space character inside single quotes to the variable space.
3
Concatenate full name
Create a variable called fullName that concatenates firstName, space, middleName, space, and lastName using square brackets [] for string concatenation.
MATLAB
Hint
Use square brackets to join strings with spaces: fullName = [firstName space middleName space lastName];
4
Display the full name
Use disp to display the value of the variable fullName.
MATLAB
Hint
Use disp(fullName) to print the full name to the screen.
Practice
(1/5)
1. Which of the following is the correct way to concatenate two strings 'Hello' and 'World' in MATLAB?
In MATLAB, square brackets [ ] are used to join strings or character arrays.
Step 2: Apply concatenation to given strings
Using ['Hello' 'World'] joins the two strings into 'HelloWorld'. Other options use incorrect syntax.
Final Answer:
['Hello' 'World'] -> Option A
Quick Check:
Use [ ] to join strings in MATLAB [OK]
Hint: Use square brackets [ ] to join strings in MATLAB [OK]
Common Mistakes:
Using + operator for strings
Using functions not defined in MATLAB
Using & operator which is invalid for strings
2. Which of the following MATLAB statements correctly concatenates the strings 'Data' and '123'?
easy
A. str = 'Data' + '123';
B. str = strcat('Data', 123);
C. str = ['Data' '123'];
D. str = 'Data' & '123';
Solution
Step 1: Check syntax for string concatenation
In MATLAB, concatenation uses square brackets [ ] for character arrays or strings.
Step 2: Evaluate each option
str = ['Data' '123']; uses ['Data' '123'] which correctly joins the two strings. str = 'Data' + '123'; uses + which is invalid. str = strcat('Data', 123); uses strcat but passes a number without converting it to string. str = 'Data' & '123'; uses & which is invalid.
Final Answer:
str = ['Data' '123']; -> Option C
Quick Check:
Concatenate strings with [ ] brackets [OK]
Hint: Use [ ] brackets to join strings, not + or & [OK]
Common Mistakes:
Using + operator for strings
Passing numbers directly to strcat without conversion
Using & operator for concatenation
3. What is the output of the following MATLAB code?
D. Error: Use square brackets [ ] instead of + for concatenation
Solution
Step 1: Identify the cause of error
Using + operator between strings in MATLAB causes an error because + is not defined for character arrays.
Step 2: Correct the concatenation method
Replace + with square brackets [ ] to join strings: result = [str1 str2];
Final Answer:
Error: Use square brackets [ ] instead of + for concatenation -> Option D
Quick Check:
Concatenate strings with [ ], not + [OK]
Hint: Replace + with [ ] to fix string concatenation error [OK]
Common Mistakes:
Using + operator for strings
Trying to use & operator
Assuming strcat always fixes + errors
5. You want to create a message in MATLAB that says 'User: John, Age: 30' by concatenating variables name = 'John' and age = 30. Which code correctly creates this message?
hard
A. msg = ['User: ' + name + ', Age: ' + string(age)];
B. msg = ['User: ' name ', Age: ' num2str(age)];
C. msg = strcat('User: ', name, ', Age: ', age);
D. msg = ['User: ' name ', Age: ' age];
Solution
Step 1: Understand variable types and concatenation
name is a string, age is a number. To concatenate, age must be converted to string using num2str.
Step 2: Check each option for correctness
msg = ['User: ' name ', Age: ' num2str(age)]; uses [ ] and num2str(age) correctly. msg = ['User: ' name ', Age: ' age]; tries to concatenate number directly causing error. msg = strcat('User: ', name, ', Age: ', age); uses strcat but passes number without conversion. msg = ['User: ' + name + ', Age: ' + string(age)]; uses + operator which is invalid.
Final Answer:
msg = ['User: ' name ', Age: ' num2str(age)]; -> Option B
Quick Check:
Convert numbers to strings before concatenation [OK]
Hint: Convert numbers to strings with num2str before concatenation [OK]
Common Mistakes:
Concatenating numbers without conversion
Using + operator for strings
Passing numbers directly to strcat
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.