I like to use cellfun to build operations instead of loops, for example, if I have several sets of sensor data and each set has several columns (due to the many sensors in the set), this is very convenient using
numOfSensors = 5; numOfSets = 6; %% sample data preparation x = 1:100; y = rand(length(x), numOfSets*numOfSensors); yCell = mat2cell(y, 100, numOfSensors*ones(1,numOfSets)); % this is my sensor data scaleCell = num2cell(fliplr(cumsum(1:numOfSets))); yCell = cellfun(@(x, scale)x.*scale, yCell, scaleCell, 'unif', false); %% plot preparation nameCell = arrayfun(@(x)['sensor set ' num2str(x)], 1:numOfSets, 'unif', false); colorCell = num2cell(lines(numOfSets), 2)'; %% plot figure, hold all, set(gca, 'ColorOrder', [0 0 0], 'LineStyleOrder', {'-','--','-*','-.',':'}) h = cellfun(@(y, name, c)plot(x, y, 'linewidth', 1.5, 'displayName', name, 'color', c), yCell, nameCell, colorCell, 'unif', false); hh = cellfun(@(x)x(1), h, 'unif', false); legend([hh{:}])
instead of a loop. In this example, all data sets are presented, each data set in it has its own color and each sensor is on one data set with another linestyle. The legend is displayed only for each data set (note: this can also be done using hggroups).
Or a simpler use case - I again have an array of data cells and want to have a short view in it:
figure, hold all, cellfun(@plot,dataCell)
What is it, one line, very fast on the command line.
Another great use case is to compress large-sized numeric data using averages (), max (), min (), std (), etc., but you already mentioned that. This becomes even more important if the data does not have the same size.