In short: Cell array is a heterogeneous container, regular array is homogeneous. This means that in a regular array all of the elements are of the same type, whereas in cell array, they can be different. You can read more about cell array here.
Use cell array when:
- You have different types in your array
- You are not sure whether in the future you might extend it to another types
- You are working with objects that have an inheritance pattern
- You are working with an array of strings - almost in any occasion it is preferable to char(n,m)
- You have a large array, and you often update a single element in a function - Due to Matlabs copy-on-write policy
- You are working with function handles (as @Pursuit explained)
Prefer regular array when:
- All of the elements have the same type
- You are updating the whole array in one shot - like mathematical operations.
- You want to have type safety
- You will not change the data type of the array in the future
- You are working with mathematical matrices.
- You are working with objects that have no inheritance
More explanation about copy-on-write:
When you pass an array to a function, a pointer/reference is passed.
function foo(x)
disp(x);
end
x= [1 2 3 4 5];
foo(x); %No copy is done here! A pointer is passed.
But when you change it (or a part of it), a copy is created.
function foo(x)
x(4) = x(4) + 1;
end
x= [1 2 3 4 5];
foo(x); %x is being copied! At least twice memory amount is needed.
In a cell array, only the cell is copied.
function foo(x)
x{4} = x{4} + 1;
end
x= {1 2 3 4 5}; %Only x{4} will be copied
Thus, if you call a function that changes a single element on a large array, you are making a lot of copies - that makes it slower. But in a cell array, it is not the case.