File Exchange

image thumbnail

Flatten (Nested) Cell Arrays

version (2.3 KB) by Yung-Yeh Chang
A helper function to flatten nested cell arrays


Updated 12 Jun 2017

View Version History

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

[out] = CELLFLAT(celllist) searches every cell element in cellist and put them on the top most level. Therefore, CELLFLAT linearizes a cell array tree structure.
Update 5/31/2017:
V2.0 -
1. The default behavior does not change.
2. Additional parameter for flatten cell arrays up to N level
cellflat(C,N) flatten cell array C for up to N level (N level search/flatten). The default value is N = -1.
3. Compatible to all Matlan versions.

Cite As

Yung-Yeh Chang (2021). Flatten (Nested) Cell Arrays (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (5)

zmi zmi

Ilya Tyuryukanov

Very useful

Chang hsiung

very handy, great submission

Yung-Yeh Chang

Thank you Iwo Bohr for valuable comment. Although error would generate if the input arguments is not a cell, I think this is useful to prompt users for the correct format. I've added the check and error message to my code.

Iwo Bohr

Very nice and useful submission. I would add one short sanity check in case of supplying an argument which is not a cell array:

if ~iscell(celllist);error('the argument must be a cell array');end

MATLAB Release Compatibility
Created with R2017a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!