Cell arrays let you store different types of data together in one container. They are useful when you want to keep numbers, text, or other data mixed in one place.
Cell array creation in MATLAB
cellArray = {item1, item2, item3, ...};Use curly braces { } to create a cell array.
Each item inside can be any data type: number, string, array, or even another cell array.
myCell = {42, 'hello', [1, 2, 3]};emptyCell = {};singleCell = {99};nestedCell = { 'text', {1, 2, 3} };This program creates a cell array with a number, a string, and a numeric array. It then prints the whole cell array and each element separately.
clc; clear; % Create a cell array with different types of data mixedCell = {123, 'MATLAB', [10, 20, 30]}; % Display the whole cell array disp('Original cell array:'); disp(mixedCell); % Access and display each element separately for index = 1:length(mixedCell) fprintf('Element %d: ', index); disp(mixedCell{index}); end
Creating a cell array is fast and uses space proportional to the number of elements.
Access elements with curly braces { } to get the content, or parentheses ( ) to get a sub-cell array.
Common mistake: Using parentheses instead of curly braces to access data inside cells returns a cell, not the data itself.
Use cell arrays when you need to store mixed data types; use regular arrays when all data is the same type.
Cell arrays hold different types of data together.
Use curly braces { } to create and access cell contents.
They are useful for flexible, mixed-type collections.
Practice
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 CQuick Check:
Curly braces create cell arrays [OK]
- Using square brackets instead of curly braces
- Using parentheses instead of curly braces
- Using angle brackets which are invalid
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 ,.Final Answer:
C = {1, 'a'; 3.14, false}; -> Option BQuick Check:
Curly braces + semicolon for rows = correct 2D cell array [OK]
- Using square brackets which create numeric arrays
- Using commas instead of semicolons for rows
- Using parentheses which are invalid for arrays
myCell = {10, 'MATLAB'; 'false', 3.5};
disp(myCell{2,1});Solution
Step 1: Understand cell content access
Using curly braces with indices accesses the content inside the cell.Step 2: Identify the element at row 2, column 1
myCell{2,1} is the element in second row, first column, which is 'false' as a string.Final Answer:
'false' -> Option AQuick Check:
myCell{2,1} = 'false' [OK]
- Using parentheses instead of curly braces to access content
- Confusing row and column indices
- Expecting the whole cell instead of content
C = {1, 2, 3; 4, 5, 6};
val = C(2,3);What is the issue with this code if you want to get the number 6 as a value?
Solution
Step 1: Understand difference between () and {}
Parentheses () return a cell array subset, curly braces {} return content.Step 2: Analyze C(2,3)
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 AQuick Check:
Use curly braces to get content: C{2,3} [OK]
- Using parentheses expecting content instead of cell
- Confusing cell array indexing with numeric array indexing
- Expecting direct numeric output from parentheses indexing
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.Final Answer:
nestedCell = {[1, 2, 3], 'text', {true, false}}; -> Option DQuick Check:
Curly braces inside curly braces create nested cells [OK]
- Using square brackets for nested cell arrays
- Using parentheses which are invalid
- Using semicolons inside nested cell arrays incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
