$npx -y skills add matlab/matlab-agentic-toolkit --skill matlab-read-medical-dataRead, write, and manipulate medical imaging data (DICOM, NIfTI, NRRD) in MATLAB. Covers Image Processing Toolbox functions (dicomreadVolume, niftiread, dicomContours, dicomanon) and Medical Imaging Toolbox enhanced APIs (medicalVolume, medicalImage, medicalref3d, extractSlice, up
| 1 | # Read and Write Medical Data |
| 2 | |
| 3 | 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. |
| 4 | |
| 5 | ## When to Use |
| 6 | |
| 7 | - Reading or writing DICOM, NIfTI, or NRRD files |
| 8 | - Listing DICOM series with `dicomCollection` |
| 9 | - Extracting spatial referencing or coordinate transforms |
| 10 | - Changing volume orientation or extracting oriented slices |
| 11 | - Working with DICOM RT structures (contours, masks, modify/write) |
| 12 | - Anonymizing DICOM data |
| 13 | |
| 14 | ## When NOT to Use |
| 15 | |
| 16 | - Displaying or visualizing volumes (use `matlab-display-volume` skill) |
| 17 | - Displaying 2-D medical images with annotations (use `matlab-display-image` skill) |
| 18 | - Reading non-medical image formats (PNG, TIFF, JPEG) — use `imread` |
| 19 | |
| 20 | ## Toolbox Detection — CRITICAL FIRST STEP |
| 21 | |
| 22 | **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. |
| 23 | |
| 24 | ## Task → Function Quick Reference |
| 25 | |
| 26 | | Task | IPT only | With Medical Imaging Toolbox (preferred) | |
| 27 | |------|----------|------------------------------------------| |
| 28 | | Read single DICOM file | `dicomread` + `dicominfo` | `medicalImage` | |
| 29 | | Read DICOM folder | `dicomreadVolume` | `medicalVolume` | |
| 30 | | Read NIfTI | `niftiread` + `niftiinfo` | `medicalVolume` | |
| 31 | | Read NRRD | — (requires MIT) | `medicalVolume` or `nrrdread` + `nrrdinfo` | |
| 32 | | List DICOM series | `dicomCollection` | `dicomCollection` | |
| 33 | | Spatial referencing | `imref3d` | `medicalref3d` | |
| 34 | | Extract oriented slice | Manual indexing | `extractSlice` | |
| 35 | | Change orientation | Manual `permute` | `updateOrientation` | |
| 36 | | Read DICOM RT structure | `dicomContours(dicominfo(file))` | Same | |
| 37 | | Anonymize DICOM | `dicomanon` + `dicomuid` | Same | |
| 38 | | Visualize volume | `volumeViewer` | `medicalVolumeViewer` or `volshow(medVol)` | |
| 39 | |
| 40 | ## Top 4 Patterns |
| 41 | |
| 42 | ### 1. Read DICOM folder |
| 43 | |
| 44 | Call `medicalVolume` or `dicomreadVolume` directly on the DICOM folder path. Do NOT call `dicomCollection` first — it is unnecessary when reading a single-series folder. |
| 45 | |
| 46 | ```matlab |
| 47 | % WITH Medical Imaging Toolbox (preferred): |
| 48 | medVol = medicalVolume("path/to/dicom/folder"); |
| 49 | V = medVol.Voxels; % Auto-rescaled (e.g., HU for CT) |
| 50 | spacing = medVol.VoxelSpacing; % [dx dy dz] in mm |
| 51 | orientation = medVol.Orientation; % "transverse", "coronal", "sagittal" |
| 52 | modality = medVol.Modality; % "CT", "MR" |
| 53 | |
| 54 | % Access spatial referencing via VolumeGeometry (medicalref3d object) |
| 55 | geom = medVol.VolumeGeometry; |
| 56 | geom.VolumeSize; % [rows cols slices] |
| 57 | geom.PatientCoordinateSystem; % "LPS+" or "RAS+" |
| 58 | geom.Position; % [slices×3] slice positions in patient coords |
| 59 | geom.VoxelDistances; % {[slices×3] [slices×3] [slices×3]} per-axis distances |
| 60 | geom.PixelSpacing; % [slices×2] in-plane pixel spacing per slice |
| 61 | geom.IsAffine; % true if uniform spacing (affine transform) |
| 62 | geom.IsAxesAligned; % true if volume axes align with patient axes |
| 63 | geom.IsMixed; % true if slices have varying pixel spacing |
| 64 | |
| 65 | % IPT only: |
| 66 | [V, spatial, dim] = dicomreadVolume("path/to/dicom/folder"); |
| 67 | V = squeeze(V); % Remove singleton 4th dimension |
| 68 | ``` |
| 69 | |
| 70 | ### 2. Read NIfTI file |
| 71 | |
| 72 | ```matlab |
| 73 | % WITH Medical Imaging Toolbox (preferred): |
| 74 | medVol = medicalVolume("path/to/file.nii.gz"); |
| 75 | |
| 76 | % IPT only: |
| 77 | V = niftiread("path/to/file.nii.gz"); |
| 78 | info = niftiinfo("path/to/file.nii.gz"); |
| 79 | voxelSize = info.PixelDimensions(1:3); |
| 80 | ``` |
| 81 | |
| 82 | ### 3. List DICOM series and read one |
| 83 | |
| 84 | Use `dicomCollection` only when: |
| 85 | - The user says the folder contains **multiple series or volumes** |
| 86 | - `medicalVolume` or `dicomreadVolume` **fails** with an error (e.g., "not a DICOM file" or "multiple volumes detected") |
| 87 | - You need to **identify what series exist** before deciding which one to read |
| 88 | |
| 89 | `dicomCollection` scans the directory, excludes non-DICOM files, and returns a table where each row is one series. It does not read pixel data. |
| 90 | |
| 91 | ```matlab |
| 92 | collection = dicomCollection("path/to/directory"); |
| 93 | disp(collection); % Table with Modality, SeriesDescription, Rows, Columns, Frames |
| 94 | |
| 95 | % WITH Medical Imaging Toolbox: |
| 96 | medVol = medicalVolume(collection, "s1"); |
| 97 | |
| 98 | % IPT only: |
| 99 | [V, spatial] = dicomreadVolume(collec |