📖 Scenario: You are organizing a small collection of different types of data about fruits. You want to store their names, colors, and average weights together in a flexible container.
🎯 Goal: Create a cell array in MATLAB that holds the fruit names, their colors, and their average weights. Then, display the cell array.
📋 What You'll Learn
Create a cell array called fruits with exactly three rows and two columns.
The first row should contain the fruit names: 'Apple' and 'Banana'.
The second row should contain the colors: 'Red' and 'Yellow'.
The third row should contain the average weights in grams: 150 and 120.
Display the entire fruits cell array using disp.
💡 Why This Matters
🌍 Real World
Cell arrays let you store mixed data types together, like names, numbers, and descriptions, which is common in data analysis and reporting.
💼 Career
Knowing how to create and manipulate cell arrays is useful for engineers and scientists who work with diverse datasets in MATLAB.
Progress0 / 4 steps
1
Create the cell array fruits
Create a cell array called fruits with three rows and two columns. The first row should have 'Apple' and 'Banana'.
MATLAB
Hint
Use curly braces {} to create a cell array. Separate rows with semicolons ;.
2
Add colors to the second row
Add the colors 'Red' and 'Yellow' to the second row of the fruits cell array.
MATLAB
Hint
Replace the empty strings in the second row with the color names.
3
Add average weights to the third row
Add the average weights 150 and 120 to the third row of the fruits cell array.
MATLAB
Hint
Replace the empty strings in the third row with the numeric weights.
4
Display the fruits cell array
Use disp(fruits) to display the entire fruits cell array.
MATLAB
Hint
Use disp to show the contents of the cell array in the command window.
Practice
(1/5)
1. What is the correct way to create a cell array in MATLAB that contains a number, a string, and a logical value?
easy
A. myCell = [42, 'hello', true];
B. myCell = (42, 'hello', true);
C. myCell = {42, 'hello', true};
D. myCell = <42, 'hello', true>;
Solution
Step 1: Understand cell array syntax
Cell arrays use curly braces { } to hold mixed data types.
Step 2: Identify correct syntax for mixed types
myCell = {42, 'hello', true}; uses curly braces with different types: number, string, logical.
Final Answer:
myCell = {42, 'hello', true}; -> Option C
Quick Check:
Curly braces create cell arrays [OK]
Hint: Use curly braces { } for cell arrays, not square brackets [ ] [OK]
Common Mistakes:
Using square brackets instead of curly braces
Using parentheses instead of curly braces
Using angle brackets which are invalid
2. Which of the following is the correct syntax to create a 2x2 cell array with elements 1, 'a', 3.14, and false?
easy
A. C = [1, 'a'; 3.14, false];
B. C = {1, 'a'; 3.14, false};
C. C = {1; 'a'; 3.14; false};
D. C = (1, 'a'; 3.14, false);
Solution
Step 1: Recall cell array 2D syntax
Use curly braces { } with semicolons for rows and commas for columns.
Step 2: Check each option
C = {1, 'a'; 3.14, false}; correctly uses { } with two rows and two columns separated by ; and ,.
C(2,3) returns a 1x1 cell containing 6, not the number 6 itself.
Final Answer:
C(2,3) returns a cell, not the content inside the cell -> Option A
Quick Check:
Use curly braces to get content: C{2,3} [OK]
Hint: Use curly braces { } to get cell content, parentheses () return cells [OK]
Common Mistakes:
Using parentheses expecting content instead of cell
Confusing cell array indexing with numeric array indexing
Expecting direct numeric output from parentheses indexing
5. You want to create a cell array that stores a numeric vector, a string, and another cell array inside it. Which of the following correctly creates such a nested cell array?
hard
A. nestedCell = {[1, 2, 3], 'text', [true; false]};
B. nestedCell = {[1, 2, 3], 'text', [true, false]};
C. nestedCell = {[1, 2, 3], 'text', (true, false)};
D. nestedCell = {[1, 2, 3], 'text', {true, false}};
Solution
Step 1: Identify nested cell array syntax
To nest a cell array inside another, use curly braces { } inside the main cell array.
Step 2: Check each option for correct nesting
nestedCell = {[1, 2, 3], 'text', {true, false}}; uses {true, false} as a nested cell array inside the main cell array.