MATLAB
cell array
data classification
programming
MATLAB tutorial

classify cell array in matlab

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Cell arrays in MATLAB are useful when your dataset contains mixed types such as numeric vectors, strings, logical values, and structs. Classification tasks often require grouping cells by type, size, or custom rules before further processing. This guide shows practical patterns for classifying cell array contents efficiently.

Build a Sample Mixed Cell Array

Start with heterogeneous input so classification logic is easy to test.

matlab
1C = {
2    42, ...
3    [1 2 3], ...
4    "hello", ...
5    true, ...
6    struct('id', 7), ...
7    {"nested"}, ...
8    pi, ...
9    datetime('now')
10};

A real workflow might load this from files, GUI forms, or preprocessing functions.

Classify by Data Type With cellfun

cellfun lets you run a function on each cell and returns a logical mask or computed value. A common first step is creating masks for numeric, text, logical, and struct elements.

matlab
1isNumeric = cellfun(@isnumeric, C);
2isString = cellfun(@(x) isstring(x) || ischar(x), C);
3isLogical = cellfun(@islogical, C);
4isStruct = cellfun(@isstruct, C);
5
6numericCells = C(isNumeric);
7textCells = C(isString);
8logicalCells = C(isLogical);
9structCells = C(isStruct);
10
11disp(numericCells)
12disp(textCells)

This approach is concise and vectorized.

Classify by Shape or Size

Sometimes type is not enough. You may need to separate scalars from vectors or matrices.

matlab
1isScalarNumeric = cellfun(@(x) isnumeric(x) && isscalar(x), C);
2isVectorNumeric = cellfun(@(x) isnumeric(x) && isvector(x) && ~isscalar(x), C);
3
4scalarNums = C(isScalarNumeric);
5vectorNums = C(isVectorNumeric);
6
7disp(scalarNums)
8disp(vectorNums)

These masks can feed later normalization steps such as converting vectors to columns or scaling scalar values.

Build Label Output for Reporting

For downstream systems, a label per cell is often easier than multiple masks.

matlab
1labels = strings(size(C));
2
3for i = 1:numel(C)
4    x = C{i};
5    if isnumeric(x)
6        if isscalar(x)
7            labels(i) = "numeric-scalar";
8        else
9            labels(i) = "numeric-array";
10        end
11    elseif isstring(x) || ischar(x)
12        labels(i) = "text";
13    elseif islogical(x)
14        labels(i) = "logical";
15    elseif isstruct(x)
16        labels(i) = "struct";
17    elseif iscell(x)
18        labels(i) = "cell";
19    else
20        labels(i) = "other";
21    end
22end
23
24table((1:numel(C))', labels', 'VariableNames', {'Index', 'ClassLabel'})

This is easy to export and audit.

Group Values Into a Struct Map

You can also collect classified values into a struct for cleaner pipeline stages.

matlab
1groups = struct();
2groups.numeric = C(cellfun(@isnumeric, C));
3groups.text = C(cellfun(@(x) isstring(x) || ischar(x), C));
4groups.logical = C(cellfun(@islogical, C));
5groups.structs = C(cellfun(@isstruct, C));
6groups.other = C(~(cellfun(@isnumeric, C) | ...
7                   cellfun(@(x) isstring(x) || ischar(x), C) | ...
8                   cellfun(@islogical, C) | ...
9                   cellfun(@isstruct, C)));
10
11disp(groups)

This can simplify later dispatch logic where each group gets processed by specialized functions.

Performance Notes

cellfun is convenient and often fast enough, but anonymous function complexity can impact performance on very large arrays. For heavy rules, a plain loop with preallocated outputs can be easier to profile and optimize.

Practical strategy:

  • start with cellfun for clarity
  • profile using timeit or profile
  • replace hotspots with specialized loops only where needed

Also avoid repeated expensive checks inside nested classification conditions. Cache reusable predicates when possible.

Validation Strategy

Classification bugs are often silent. Add unit-like checks for representative inputs:

  • empty cell array
  • all numeric values
  • mixed nested cells
  • unexpected object types

Simple assertions can prevent downstream failures:

matlab
assert(numel(labels) == numel(C), 'Label count mismatch');
assert(all(labels ~= ""), 'Unclassified element detected');

This is especially important when classification drives automated data transformations.

Common Pitfalls

  • Assuming all text entries are one type while mixing char arrays and string scalars.
  • Using cellfun expressions that fail on nested complex objects.
  • Recomputing many masks repeatedly instead of storing them once.
  • Returning inconsistent label formats that break downstream joins.
  • Skipping edge-case validation for empty or deeply nested cells.

Summary

  • Classify MATLAB cell arrays with cellfun masks and custom predicates.
  • Separate type-based and shape-based classification rules clearly.
  • Produce explicit labels or grouped structs for maintainable pipelines.
  • Profile large workloads before micro-optimizing implementation style.
  • Add assertions and edge-case tests to keep classification reliable.

Course illustration
Course illustration

All Rights Reserved.