each

バージョン 1.0.0.0 (1.36 KB) 作成者: Robert Rasche
iterate cell array contents without clutter
ダウンロード: 19
更新 2017/11/22

ライセンスの表示

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.

引用

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

MATLAB リリースの互換性
作成: R2012b
すべてのリリースと互換性あり
プラットフォームの互換性
Windows macOS Linux
カテゴリ
Help Center および MATLAB AnswersData Type Identification についてさらに検索

Community Treasure Hunt

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

Start Hunting!
バージョン 公開済み リリース ノート
1.0.0.0