Lightweight Enumeration

Version 1.0.0 (1.35 KB) by Shadow
Enumeration, i.e ForEach + Index. A small anonymous one liner, that allows for better readability of indices.
0 Downloads
Updated 13 Jun 2024

View License

% LIGHTWEIGHT_ENUMERATION by Shadow in 2024
% new command to improve readability of indexed for loops in code
enumerate = @(my_array) cell2mat(arrayfun(@(x,idx) struct("cargo",x,"idx",idx), my_array(:).', 1:numel(my_array(:).'),UniformOutput=false));
% SAMPLE CODE
clc
random_stuff = rand(1, 3)
column_list = (33:37).'
for outer = enumerate(column_list)
disp("Item Number " + string(outer.idx) +" contains the numer " + string(outer.cargo) + ".")
for inner = enumerate(random_stuff)
disp(" " + string(outer.idx) + ": random nr. " + string(inner.idx) + " is: " + string(inner.cargo) + ".")
end
end

Cite As

Shadow (2026). Lightweight Enumeration (https://www.mathworks.com/matlabcentral/fileexchange/168011-lightweight-enumeration), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2024a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags
Version Published Release Notes
1.0.0