Cell arrays hold different types of data together. Using curly braces or parentheses helps you get data or parts of the cell array in different ways.
Cell array indexing (curly vs parentheses) in MATLAB
classdef CellArrayExample
properties
cells
end
methods
function obj = CellArrayExample()
obj.cells = {1, 'text'; [2 3], magic(2)};
end
function content = getContent(obj, row, col)
content = obj.cells{row, col}; % Curly braces to get content
end
function subCells = getSubCells(obj, rows, cols)
subCells = obj.cells(rows, cols); % Parentheses to get sub-cell array
end
end
endCurly braces { } extract the actual data inside the cell.
Parentheses ( ) extract a cell or cells as a smaller cell array.
myCells = {10, 'hello'; [1 2], {5, 6}};
value = myCells{1,2}; % Gets 'hello' as a stringsubCells = myCells(1, 2); % Gets a 1x1 cell array containing 'hello'
emptyCells = {};
% Trying to get content from empty cell array will cause error
% content = emptyCells{1}; % This will error
subCells = emptyCells([]); % This returns an empty cell arraysingleCell = {42};
content = singleCell{1}; % Gets 42
subCell = singleCell(1); % Gets a 1x1 cell array containing 42This program shows how to use parentheses to get a smaller cell array and curly braces to get or change the actual content inside cells.
clc; clear; % Create a cell array with different types of data myCellArray = {123, 'apple'; [4 5 6], magic(3)}; % Display original cell array disp('Original cell array:'); disp(myCellArray); % Use parentheses to get a sub-cell array (1x2) subCellArray = myCellArray(1, :); disp('Sub-cell array using parentheses (1, :):'); disp(subCellArray); % Use curly braces to get content inside a cell content1 = myCellArray{1, 2}; % 'apple' content2 = myCellArray{2, 1}; % [4 5 6] fprintf('Content at (1,2) using curly braces: %s\n', content1); fprintf('Content at (2,1) using curly braces: [%s]\n', num2str(content2)); % Modify content inside a cell using curly braces myCellArray{1,1} = 999; disp('Cell array after changing (1,1) content to 999:'); disp(myCellArray);
Using curly braces to get content is fast and direct, but you get the raw data, not a cell.
Parentheses keep the data inside cells grouped as cells, useful when you want to keep the structure.
Trying to use curly braces on an empty cell or invalid index causes an error.
Use curly braces when you want to work with the actual data inside cells.
Use parentheses when you want to keep the data inside cells grouped or want a smaller cell array.
Curly braces { } get or set the actual content inside a cell.
Parentheses ( ) get or set a smaller cell array, keeping the cell structure.
Use curly braces to work with data inside cells, parentheses to work with cells themselves.
Practice
{} do when indexing a cell array in MATLAB?Solution
Step 1: Understand curly brace indexing
Curly braces{}in MATLAB cell arrays access the content inside the cell, not the cell itself.Step 2: Compare with parentheses
Parentheses()return a cell array containing the selected cells, but curly braces extract the actual data.Final Answer:
It accesses the actual content inside the cell. -> Option CQuick Check:
Curly braces = content access [OK]
- Confusing curly braces with parentheses
- Thinking curly braces return a cell array
- Assuming curly braces delete cells
C?Solution
Step 1: Identify correct indexing for content extraction
Curly braces{}are used to extract content from a cell. SoC{1}extracts the content of the first cell.Step 2: Check other options
C(1)returns a cell array, not content.C[1]is invalid syntax in MATLAB.C{1,1}is valid only ifCis 2D, butC{1}is standard for 1D cell arrays.Final Answer:
content = C{1}; -> Option AQuick Check:
Curly braces extract content [OK]
- Using parentheses instead of curly braces
- Using square brackets which are invalid
- Confusing 1D and 2D cell indexing
C = {10, 20; 30, 40}; what is the output of result = C(1,2);?Solution
Step 1: Understand parentheses indexing on cell arrays
Using parentheses()returns a cell array containing the selected cells, not the content inside.Step 2: Apply to given code
C(1,2)returns a 1x1 cell array containing the element at row 1, column 2, which is 20 inside a cell.Final Answer:
result is a 1x1 cell array containing 20 -> Option DQuick Check:
Parentheses return cell array [OK]
- Thinking parentheses extract content
- Confusing row and column indices
- Expecting a numeric output instead of cell
C = {1, 2, 3};
val = C(2);
disp(val + 1);What is the error in this code?
Solution
Step 1: Identify the type of val after indexing
val = C(2);uses parentheses, sovalis a 1x1 cell array, not a number.Step 2: Understand why adding 1 causes error
Adding 1 to a cell array is invalid. To add 1 to the content, use curly braces:val = C{2};Final Answer:
Cannot add 1 to a cell array; use curly braces to get content. -> Option AQuick Check:
Parentheses return cell, cannot add number [OK]
- Using parentheses when curly braces needed
- Assuming disp cannot show numbers
- Thinking parentheses indexing is syntax error
C = {1, {2, 3}, 4}; and want to extract the number 3 inside the nested cell. Which indexing expression correctly extracts it?Solution
Step 1: Understand the structure of C
Cis a 1x3 cell array: first cell is 1, second cell is another cell array {2,3}, third cell is 4.Step 2: Extract nested content step-by-step
UseC{2}to get the nested cell array {2,3}, then{2}to get the second element inside it, which is 3. SoC{2}{2}extracts 3.Final Answer:
C{2}{2} -> Option BQuick Check:
Curly braces twice for nested content [OK]
- Using parentheses which return cells, not content
- Mixing parentheses and curly braces incorrectly
- Trying to index nested cell without extracting first
Ready to prove it?
Pass the test and this pattern moves unknown
