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.