matlab-read-medical-data
>
git clone --depth 1 https://github.com/matlab/matlab-agentic-toolkit /tmp/matlab-read-medical-data && cp -r /tmp/matlab-read-medical-data/skills-catalog/image-processing-and-computer-vision/matlab-read-medical-data ~/.claude/skills/matlab-read-medical-dataSKILL.md
# Read and Write Medical Data
Read, write, and manipulate medical imaging data in MATLAB. This skill covers both Image Processing Toolbox (IPT) functions and Medical Imaging Toolbox (MIT) enhanced APIs.
## When to Use
- Reading or writing DICOM, NIfTI, or NRRD files
- Listing DICOM series with `dicomCollection`
- Extracting spatial referencing or coordinate transforms
- Changing volume orientation or extracting oriented slices
- Working with DICOM RT structures (contours, masks, modify/write)
- Anonymizing DICOM data
## When NOT to Use
- Displaying or visualizing volumes (use `matlab-display-volume` skill)
- Displaying 2-D medical images with annotations (use `matlab-display-image` skill)
- Reading non-medical image formats (PNG, TIFF, JPEG) — use `imread`
## Toolbox Detection — CRITICAL FIRST STEP
**Always check which toolboxes are available before choosing an approach.** If Medical Imaging Toolbox is installed, prefer its APIs. If only Image Processing Toolbox is available, use IPT patterns.
## Task → Function Quick Reference
| Task | IPT only | With Medical Imaging Toolbox (preferred) |
|------|----------|------------------------------------------|
| Read single DICOM file | `dicomread` + `dicominfo` | `medicalImage` |
| Read DICOM folder | `dicomreadVolume` | `medicalVolume` |
| Read NIfTI | `niftiread` + `niftiinfo` | `medicalVolume` |
| Read NRRD | — (requires MIT) | `medicalVolume` or `nrrdread` + `nrrdinfo` |
| List DICOM series | `dicomCollection` | `dicomCollection` |
| Spatial referencing | `imref3d` | `medicalref3d` |
| Extract oriented slice | Manual indexing | `extractSlice` |
| Change orientation | Manual `permute` | `updateOrientation` |
| Read DICOM RT structure | `dicomContours(dicominfo(file))` | Same |
| Anonymize DICOM | `dicomanon` + `dicomuid` | Same |
| Visualize volume | `volumeViewer` | `medicalVolumeViewer` or `volshow(medVol)` |
## Top 4 Patterns
### 1. Read DICOM folder
Call `medicalVolume` or `dicomreadVolume` directly on the DICOM folder path. Do NOT call `dicomCollection` first — it is unnecessary when reading a single-series folder.
```matlab
% WITH Medical Imaging Toolbox (preferred):
medVol = medicalVolume("path/to/dicom/folder");
V = medVol.Voxels; % Auto-rescaled (e.g., HU for CT)
spacing = medVol.VoxelSpacing; % [dx dy dz] in mm
orientation = medVol.Orientation; % "transverse", "coronal", "sagittal"
modality = medVol.Modality; % "CT", "MR"
% Access spatial referencing via VolumeGeometry (medicalref3d object)
geom = medVol.VolumeGeometry;
geom.VolumeSize; % [rows cols slices]
geom.PatientCoordinateSystem; % "LPS+" or "RAS+"
geom.Position; % [slices×3] slice positions in patient coords
geom.VoxelDistances; % {[slices×3] [slices×3] [slices×3]} per-axis distances
geom.PixelSpacing; % [slices×2] in-plane pixel spacing per slice
geom.IsAffine; % true if uniform spacing (affine transform)
geom.IsAxesAligned; % true if volume axes align with patient axes
geom.IsMixed; % true if slices have varying pixel spacing
% IPT only:
[V, spatial, dim] = dicomreadVolume("path/to/dicom/folder");
V = squeeze(V); % Remove singleton 4th dimension
```
### 2. Read NIfTI file
```matlab
% WITH Medical Imaging Toolbox (preferred):
medVol = medicalVolume("path/to/file.nii.gz");
% IPT only:
V = niftiread("path/to/file.nii.gz");
info = niftiinfo("path/to/file.nii.gz");
voxelSize = info.PixelDimensions(1:3);
```
### 3. List DICOM series and read one
Use `dicomCollection` only when:
- The user says the folder contains **multiple series or volumes**
- `medicalVolume` or `dicomreadVolume` **fails** with an error (e.g., "not a DICOM file" or "multiple volumes detected")
- You need to **identify what series exist** before deciding which one to read
`dicomCollection` scans the directory, excludes non-DICOM files, and returns a table where each row is one series. It does not read pixel data.
```matlab
collection = dicomCollection("path/to/directory");
disp(collection); % Table with Modality, SeriesDescription, Rows, Columns, Frames
% WITH Medical Imaging Toolbox:
medVol = medicalVolume(collection, "s1");
% IPT only:
[V, spatial] = dicomreadVolume(collection, "s1");
```
### 4. Extract slice / change orientation (Requires Medical Imaging Toolbox)
```matlab
medVol = medicalVolume("path/to/file.nii");
% Extract slices — works for any orientation
[axialSlice, position, spacings] = extractSlice(medVol, 50, "transverse");
[coronalSlice, ~, ~] = extractSlice(medVol, 30, "coronal");
[sagittalSlice, ~, ~] = extractSlice(medVol, 45, "sagittal");
% If medVol.Orientation is not empty, use it as the third input
[sliceData, position, spacings] = extractSlice(medVol, 50, medVol.Orientation);
% Change orientation — do NOT use permute
medVolCoronal = updateOrientation(medVol, "coronal"); % Returns NEW object
```
`updateOrientation` was introduced in **R2025a**.
## Detailed Reference Files
**IMPORTANT: Before generating code for any task below, read the matching reference file first.**
| Task trigger | Reference | Read BEFORE |
|--------------|-----------|-------------|
| Reading/writing DICOM or NIfTI with IPT | `references/ipt-reading-writing.md` | Writing `dicomreadVolume`, `niftiread`, `imref3d`, or rescale logic |
| Using `medicalVolume`, `medicalImage`, slices, or orientation | `references/mit-medical-volume.md` | Writing `medicalImage`, `medicalVolume`, `extractSlice`, or `updateOrientation` calls |
| Spatial referencing or coordinate transforms | `references/mit-spatial-referencing.md` | Writing `medicalref3d`, `intrinsicToWorld`, or `worldToIntrinsic` calls |
| RT structures (contours, labelmaps, RTSTRUCT) | `references/dicom-rt-workflows.md` | Reading, editing, displaying, or plotting contours/RTSTRUCT files, or writing any `dicomContours`, `plotContour`, `createMask`, `addContour`, `deleteContour` call |
| Anonymizing DI>
Analyze AMS waveform data using Mixed-Signal Blockset utilities: phase noise measurement, clock jitter, anti-aliased resampling, timing measurements, lock time, INL/DNL, ADC/DAC calibration, HSpice import. Use when analyzing time-domain voltage from PLL/VCO/clock simulations, measuring phase noise from variable-step solver output, computing jitter, or resampling non-uniform data.
Analyze data using MATLAB. Use when the task involves tables, timetables, time-series data, numeric arrays, sensor matrices, or gridded data — including but not limited to exploring, filtering, sorting, cleaning, transforming, aggregating, smoothing, padding, trimming, and answering questions about data. MATLAB provides extensive, easy-to-use built-in functions for these workflows with no additional products required.
Analyze the effective toolbox file set to produce a Dependency Manifest — classify all transitive dependencies as included, product, add-on, or external-unresolved, then present resolution options with tradeoffs. Use after matlab-define-toolbox-api when the spec is approved.
S-parameters, insertion loss, fields, currents, mesh control, and solver selection for RF PCB performance validation. TRIGGER: user asks to compute S-parameters, analyze insertion/return loss, extract fields or currents, compare MoM vs FEM, or control mesh for any RF PCB component. Invoke BEFORE writing sparameters() or solver code — API is non-obvious. SKIP: designing or creating components (use the specific matlab-design-pcb-* skill), material/stackup setup only (use matlab-manage-pcb-material), optimization sweeps (use matlab-optimize-pcb-design), PDN/IR-drop analysis (use matlab-analyze-pcb-pdn).
Analyze antennas installed on electrically large conducting platforms using MATLAB Antenna Toolbox. Loads platform geometry from STL/STEP/IGES, installs antenna elements, selects electromagnetic solvers (MoM-PO, FMM, MoM), and computes patterns, impedance, coupling, and efficiency. Use when the user wants to model an antenna on a vehicle, aircraft, ship, satellite, or other large structure.
PDN DC voltage/current analysis, IR drop, design rule checking, and multi-net batch analysis on imported PCB layouts. TRIGGER: user asks about power integrity, PDN analysis, IR drop, voltage distribution, current density, power nets, or design rule checking on a PCB. Invoke BEFORE writing code — the PDN API chain is specialized and non-obvious. SKIP: importing a PCB file (use matlab-read-pcb-layout), EM field/S-parameter extraction (use matlab-analyze-em), material/stackup setup only (use matlab-manage-pcb-material), transmission line design (use matlab-design-pcb-txline).
Calculate and visualize monostatic and bistatic radar cross section (RCS) using MATLAB Antenna Toolbox. Computes RCS of platforms, antennas, and arrays with PO, MoM, and FMM solvers, supporting HH/VV/HV/VH polarization, GPU acceleration, and near-field observation. Use when the user wants to compute, plot, or analyze radar cross section.