0
0
MATLABdata~5 mins

Cell array indexing (curly vs parentheses) in MATLAB

Choose your learning style9 modes available
Introduction

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.

When you want to get the actual content inside a cell.
When you want to get a smaller cell array from a bigger one.
When you want to change the content inside a cell.
When you want to keep the data inside cells grouped as cells.
Syntax
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
end

Curly braces { } extract the actual data inside the cell.

Parentheses ( ) extract a cell or cells as a smaller cell array.

Examples
Curly braces get the content inside the cell at row 1, column 2.
MATLAB
myCells = {10, 'hello'; [1 2], {5, 6}};
value = myCells{1,2}; % Gets 'hello' as a string
Parentheses get a smaller cell array, not the content inside.
MATLAB
subCells = myCells(1, 2); % Gets a 1x1 cell array containing 'hello'
Curly braces on empty cell cause error; parentheses return empty cell array.
MATLAB
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 array
With one element, curly braces get the value, parentheses get a cell array.
MATLAB
singleCell = {42};
content = singleCell{1}; % Gets 42
subCell = singleCell(1); % Gets a 1x1 cell array containing 42
Sample Program

This program shows how to use parentheses to get a smaller cell array and curly braces to get or change the actual content inside cells.

MATLAB
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);
OutputSuccess
Important Notes

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.

Summary

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.