each

iterate cell array contents without clutter
19 Downloads
Updated 22 Nov 2017

View License

The "each"-utility allows to iterate the contents of a cell array using a for-loop almost like one would do with any numeric or struct array that isn't in cells.
Normally, when using a for loop, the cell array is passed to the loop variable in columns (that are Nx1-cell arrays).

for elem = {'a', 'list', 'of', 'words'}
class(elem) % -> cell
size(elem) % -> 1x1
disp(elem{1}); % need unpack
end

This is inconvenient, when using only row-vector lists of things. One would want the for-loop to directly assign the 1x1-colum-cell's content to the loop variable. Passing the cell through the constructor of the "each"-utility, you can achieve just that.

for elem = each({'a', 'list', 'of', 'words'})
class(elem) % -> char
size(elem) % -> 1xN (length of word)
disp(elem); % need not unpack
end

It also looks very clean, i think.

One restriction is, that "each" will pass out the contents of all n*m 1x1-cells of the original nxm-cell-array one after another, rather than passing a column at a time - there is after all only one variable to receive the result. For clarity, you should therefore only use it with row-vectors.

Under the hood, "each" is a thin class (whos instances are only temporary). It wraps the cell-array containing the data and provides size- and subsref-handlers that suite the way MATLAB internally does for-loops.

Cite As

Robert Rasche (2024). each (https://www.mathworks.com/matlabcentral/fileexchange/65149-each), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2012b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Data Type Identification in Help Center and MATLAB Answers

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes
1.0.0.0