📖 Scenario: Imagine you are working with a small store's sales data. The sales numbers are stored as strings, but you need to convert them to numbers to do calculations.
🎯 Goal: You will create a list of sales as strings, convert them to numbers, and then calculate the total sales.
📋 What You'll Learn
Create a cell array of sales amounts as strings
Create a variable to hold the number of sales
Convert the sales strings to numbers using str2double
Calculate the total sales by summing the numeric values
Print the total sales
💡 Why This Matters
🌍 Real World
Stores often receive sales data as text from different sources. Converting these texts to numbers is essential for calculations like totals and averages.
💼 Career
Data analysts and programmers frequently convert data types to prepare data for analysis and reporting.
Progress0 / 4 steps
1
Create sales data as strings
Create a cell array called salesStrings with these exact string values: '100', '250', '175', '300', '225'.
MATLAB
Hint
Use curly braces {} to create a cell array of strings in MATLAB.
2
Create a variable for the number of sales
Create a variable called numSales that stores the number of elements in salesStrings using the length function.
MATLAB
Hint
Use length(salesStrings) to find how many sales there are.
3
Convert sales strings to numbers
Create a variable called salesNumbers that converts salesStrings to numeric values using the str2double function.
MATLAB
Hint
Use str2double to convert each string in the cell array to a number.
4
Calculate and display total sales
Calculate the total sales by summing salesNumbers and store it in a variable called totalSales. Then, print the total sales using disp with the message: 'Total sales: ' followed by the number.
MATLAB
Hint
Use sum to add numbers and disp with num2str to print the message and number.
Practice
(1/5)
1. Which MATLAB function converts a string like '123.45' into a numeric double value?
easy
A. char
B. int32
C. str2double
D. num2str
Solution
Step 1: Understand the input and desired output
The input is a string representing a number, and we want a numeric double value.
Step 2: Identify the correct conversion function
str2double converts strings to double precision numbers, while int32 converts numbers to integers, char converts numbers to characters, and num2str converts numbers to strings.
Final Answer:
str2double -> Option C
Quick Check:
String to double = str2double [OK]
Hint: Use str2double for string to number conversion [OK]
Common Mistakes:
Using int32 on strings causes errors
Using char converts numbers to characters, not numbers
Using num2str converts numbers to strings, not the other way
2. Which of the following is the correct syntax to convert the number 3.7 to an integer in MATLAB?
MATLAB uses functions like int32, int16, etc., to convert numbers to integers.
Step 2: Check syntax correctness
int32(3.7) is valid syntax; the others are not MATLAB functions.
Final Answer:
int32(3.7) -> Option A
Quick Check:
Correct integer conversion = int32(3.7) [OK]
Hint: Use int32() to convert decimals to integers [OK]
Common Mistakes:
Using non-existent functions like int() or integer()
Forgetting parentheses in function calls
Expecting rounding instead of truncation
3. What is the output of the following MATLAB code?
val = int32(5.9);
disp(val);
medium
A. 5
B. Error
C. 5.9
D. 6
Solution
Step 1: Understand int32 conversion behavior
Converting a decimal number to int32 truncates the decimal part, it does not round.
Step 2: Apply truncation to 5.9
Truncating 5.9 results in 5, so val becomes 5.
Final Answer:
5 -> Option A
Quick Check:
int32 truncates decimals = 5 [OK]
Hint: int32 cuts decimals, does not round [OK]
Common Mistakes:
Assuming int32 rounds up
Expecting output to be 5.9 (no conversion)
Thinking it causes an error
4. The following MATLAB code throws an error. What is the problem?
num = '123';
val = int32(num);
medium
A. Variable name 'num' is reserved
B. int32 cannot convert strings directly
C. Missing semicolon after assignment
D. int32 only works with integers, not strings
Solution
Step 1: Analyze the input type
The variable num is a string, not a numeric value.
Step 2: Check int32 input requirements
int32 expects numeric input, not strings. To convert a string to a number, use str2double first.
Final Answer:
int32 cannot convert strings directly -> Option B
Quick Check:
int32 needs numeric input [OK]
Hint: Convert string to number before int32 [OK]
Common Mistakes:
Trying to convert string directly with int32
Ignoring type mismatch errors
Assuming int32 rounds strings
5. You have a cell array of strings representing numbers: {'10.5', '20.1', '30.9'}. Which MATLAB code correctly converts this to an integer array by truncating decimals?
hard
A. intArray = str2double(int32(cellArray));
B. intArray = int32(cellArray);
C. intArray = int32(str2num(cellArray));
D. intArray = int32(cellfun(@str2double, cellArray));
Solution
Step 1: Understand cell array conversion
Each string in the cell array must be converted individually to a number.
Step 2: Use cellfun with str2double and then int32
cellfun(@str2double, cellArray) converts each string to double, then int32 truncates decimals for the whole array.
Step 3: Check other options
intArray = str2double(int32(cellArray)); tries to convert the cell array to int32 first, which fails. intArray = int32(cellArray); tries int32 on strings directly, which errors. intArray = int32(str2num(cellArray)); uses str2num incorrectly on cell array.
Final Answer:
intArray = int32(cellfun(@str2double, cellArray)); -> Option D
Quick Check:
Use cellfun + str2double + int32 [OK]
Hint: Use cellfun with str2double before int32 [OK]
Common Mistakes:
Trying to convert whole cell array at once
Using int32 directly on strings
Confusing str2num with str2double on cell arrays
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.