each

iterate cell array contents without clutter
19 download
Aggiornato 22 nov 2017

Visualizza la licenza

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.

Cita come

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

Compatibilità della release di MATLAB
Creato con R2012b
Compatibile con qualsiasi release
Compatibilità della piattaforma
Windows macOS Linux
Categorie
Scopri di più su Data Type Identification in Help Center e MATLAB Answers

Community Treasure Hunt

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

Start Hunting!
Versione Pubblicato Note della release
1.0.0.0