MPath accesses
deeply
nested
data with paths

Retrieve and modify data in nested structs, tables, and objects in MATLAB. You can reduce or eliminate:

  • nested loops
  • temporary variables
  • hardcoded sibling member names
  • anonymous functions
  • lengthy boilerplate code

Retrieve height and mass for every subject with MPath:

  • data: struct
    • Subject1, Subject2, Subject3: struct
      • Height, Mass: double
      • Note, Status: string
      • SelfReport: table
        • Difficulty, Fatigue: double

Subject* matches all subject names.

{Height,Mass} selects both measurements.

>> mpath.get(data, ...
    "/Subject*/{Height,Mass}")

3×2 double

    1.72    68.5
    1.80    74.2
    1.65    61.8

Installation

Requires MATLAB R2025a or later. Choose the method that fits your MATLAB workflow.

MATLAB add-on

Download the .mltbx file, open it in MATLAB, and approve the installation. MATLAB keeps the installed add-on available across sessions.

Download MATLAB add-on (.mltbx)
ZIP archive

Download and extract the ZIP, change MATLAB's working directory to the extracted MPath folder, then:

For this and future sessions, run:

setup --savepath user-startup

For this session only, run:

setup()
Download ZIP archive
Clone source

In a terminal, clone the repository:

git clone https://git.usercurt.com/UserCurt/mpath.git

Change the working directory to the cloned MPath folder, then:

For this and future sessions, run:

setup --savepath user-startup

For this session only, run:

setup()

To access the installed documentation, run doc in MATLAB, expand Supplemental Software, and select MPath Toolbox.

Usage examples

Each example starts from a fresh copy of the sample data and is independent of the others.

Sample hierarchy

Example data for three study participants: height and mass, experiment notes and completion status, and self-reported difficulty and fatigue.

  • data: struct
    • Subject1, Subject2, Subject3: struct
      • Height, Mass: double
      • Note, Status: string
      • SelfReport: table
        • Difficulty, Fatigue: double
Retrieve masses

Retrieve masses across subjects

Subject? matches subject names with exactly one character after Subject, retrieving each matching subject's mass.

Without MPath

masses = [
    data.Subject1.Mass
    data.Subject2.Mass
    data.Subject3.Mass
]

With MPath

masses = mpath.get( ...
    data, "/Subject?/Mass")
Result
masses =

   68.5000
   74.2000
   61.8000
Read across levels

Read self-report ratings across levels

Subject? matches the subjects; * matches every variable in each self-report table.

Without MPath

subjects = fieldnames(data);
ratings = zeros(3, 2);

for i = 1:3
    subject = data.(subjects{i});
    ratings(i,:) = ...
        subject.SelfReport{1,:};
end

ratings

With MPath

ratings = mpath.get(data, ...
    "/Subject?/SelfReport/*")
Result
ratings =

     4     5
     3     4
     5     4
Set selected notes

Set notes for selected subjects

{Subject1,Subject3} selects two subjects and assigns the same note to both, leaving the remaining note unchanged.

Without MPath

data.Subject1.Note = "Follow-up required";
data.Subject3.Note = "Follow-up required";

notes = [
    data.Subject1.Note
    data.Subject2.Note
    data.Subject3.Note
]

With MPath

data = mpath.set( ...
    data, "/{Subject1,Subject3}/Note", ...
    "Follow-up required");

notes = mpath.get(data, ...
    "/Subject?/Note")
Result
notes =

  3×1 string array

    "Follow-up required"
    "Check calibration"
    "Follow-up required"

Core access functions

MPath provides three query functions for inspecting and retrieving nested data and two mutation functions for returning modified data.

Query functions

  • mpath.getretrieve values
  • mpath.existscheck whether paths exist
  • mpath.resolveshow matching locations

Mutation functions

  • mpath.setassign values
  • mpath.removeremove members