FUNCTION REFERENCE

dicomwrite

Write a native (uncompressed) grayscale, RGB or multiframe DICOM Part 10 file atomically.

Write a native (uncompressed) grayscale, RGB or multiframe DICOM Part 10 file atomically.

Syntax

dicomwrite(X,filename); dicomwrite(X,filename,metadata); dicomwrite(X,filename,info,'CreateMode','copy'); dicomwrite(___,Name,Value)

X must be nonempty int8, int16, uint8 or uint16; values are preserved exactly. Shapes: R-by-C grayscale (MONOCHROME2, one sample per pixel); R-by-C-by-3 truecolor (uint8 or uint16 only: SamplesPerPixel 3, PhotometricInterpretation RGB, PlanarConfiguration 0, red, green and blue stored together for each pixel); R-by-C-by-1-by-F grayscale frames and R-by-C-by-3-by-F truecolor frames (NumberOfFrames F, all frames in one file). A 3-D array whose third dimension is not 3 errors and names the 4-D form; other ranks error. Signed classes are written as two's complement with PixelRepresentation 1. Rows, Columns, SamplesPerPixel, PhotometricInterpretation, PlanarConfiguration, NumberOfFrames, BitsAllocated, BitsStored, HighBit and PixelRepresentation are derived from X; supplied values must agree, and the derived values are what the file contains (PlanarConfiguration only for RGB, NumberOfFrames only for more than one frame). BitsStored equals BitsAllocated (8 or 16). Relative paths use Current Folder. Statement-only: requesting any output errors before writing.

Name/value options (names and text values are case-insensitive; a repeated option errors): TransferSyntax (UID), VR ('implicit' or 'explicit'), Endian ('ieee-le' or 'ieee-be'), CreateMode ('create', the default, or 'copy') and WritePrivate (logical, default false). The three native transfer syntaxes are explicit VR little endian (1.2.840.10008.1.2.1, the default), implicit VR little endian (1.2.840.10008.1.2) and retired explicit VR big endian (1.2.840.10008.1.2.2). VR and Endian choose among them; a given TransferSyntax overrides both; VR 'implicit' with Endian 'ieee-be' and no TransferSyntax errors. File meta is always explicit little endian. CompressionMode 'None', UseMetadataBitDepths false, MultiframeSingleFile true, ObjectType 'Secondary Capture Image Storage' and SOPClassUID 1.2.840.10008.5.1.4.1.1.7 are accepted as their documented defaults and change nothing; any other value of these options errors as not supported yet and names the option.

Metadata must be one scalar struct. Allowed text fields: PatientName (DICOM PN text or the component struct returned by dicominfo), PatientID, StudyDate (empty or YYYYMMDD), StudyTime (empty or HH, HHMM, HHMMSS with optional 1-6 fractional second digits), Modality, StudyDescription, SeriesDescription, SOPClassUID, SOPInstanceUID, StudyInstanceUID and SeriesInstanceUID. SeriesNumber and InstanceNumber accept empty/text IS or a numeric signed 32-bit integer scalar. The derived numeric pixel fields accept real integer scalars; NumberOfFrames also accepts integer text. Text is VM=1, with VR length/format checks; NUL, control characters, backslash multiplicity and caller-supplied trailing space padding are rejected. PN permits up to three 64-character groups and five components per group. LO permits 64 characters. SpecificCharacterSet accepts empty/ISO_IR 6 (ASCII) or ISO_IR 192 (UTF-8); non-ASCII text automatically adds ISO_IR 192 when absent, and contradicting explicit ASCII declarations error.

Only Secondary Capture Image Storage SOPClassUID 1.2.840.10008.5.1.4.1.1.7 is accepted. Missing SOPInstanceUID, StudyInstanceUID and SeriesInstanceUID are fresh OS-random UUIDv4 decimal 2.25 UIDs; explicit valid UIDs are preserved. File meta storage UIDs match the dataset. ImplementationClassUID is the stable deterministic UUID-derived 2.25.96426248368486820571789363904423688279 (derivation in tool/refs/dicom/SOURCES.md).

SC bookkeeping defaults: Modality OT, ConversionType WSD; empty PatientName, PatientID, PatientBirthDate, PatientSex, StudyDate, StudyTime, AccessionNumber, ReferringPhysicianName, StudyID, SeriesNumber, InstanceNumber and PatientOrientation when absent. These defaults do not constitute clinical IOD conformance or validation of arbitrary clinical metadata. In create mode recognized DICOM fields outside the create set and dicominfo bookkeeping fields are dropped silently, so dicomwrite(X,filename,dicominfo(source)) is accepted when the retained metadata agrees with X and the supported Secondary Capture subset. Unknown field names error and name the field; option names used as struct fields remain unsupported. Use CreateMode 'copy' to preserve other attributes.

CreateMode 'copy' takes the scalar struct dicominfo returns (or one built by hand) and writes every field that names an attribute of the factory dictionary, including repeating-group names such as OverlayRows_2, with the dictionary VR. Nothing is checked against an IOD and no missing value or UID is generated. Text VRs take a character vector or string scalar (several values separated by backslashes). DS and IS also take numeric arrays; DS is written as the shortest decimal that fits 16 bytes, so a double that needs an exponent keeps about 12 significant digits. PN takes text or the dicominfo struct form (FamilyName, GivenName, MiddleName, NamePrefix, NameSuffix, optional Ideographic and Phonetic structs, one struct element per name). US, SS, UL, SL, UV, SV, FL, FD and AT take numeric arrays (AT as 65536*group+element). OB, OW, OL, OV, OF, OD and UN take numeric arrays as raw cells. A sequence is a scalar struct with fields Item_1 ... Item_n, each a scalar struct, written with defined lengths. Attributes listed as US or SS follow PixelRepresentation of the written image, or of the enclosing item when it has one. For LUTDescriptor and the red, green and blue palette descriptors with VR SS, the first and third values are unsigned 16-bit integers and only the second is signed. Attributes that describe X always come from X, whatever the struct says: Rows, Columns, SamplesPerPixel, PhotometricInterpretation (MONOCHROME1 in the struct is kept for grayscale X), PlanarConfiguration, NumberOfFrames, BitsAllocated, BitsStored, HighBit, PixelRepresentation and PixelData. File meta (group 0002) is regenerated: TransferSyntaxUID from the written syntax, the MediaStorage UIDs from SOPClassUID and SOPInstanceUID, or from the struct's MediaStorageSOPClassUID and MediaStorageSOPInstanceUID when those are absent; with neither the call errors. Ignored silently: the dicominfo bookkeeping fields Filename, FileModDate, FileSize, Format, FormatVersion, Width, Height, BitDepth, ColorType and StartOfPixelData, every field whose name contains GroupLength, and other group 7FE0 fields. Private fields (Private_gggg_eeee and Private_gggg_eexx_Creator, hexadecimal) are dropped unless WritePrivate is true; then creators are written as LO and other private data as UN (numeric bytes or text), or with the VR the active custom dictionary gives that private tag. Any other field name errors and names the field. Text must be ASCII or, for LO, LT, PN, SH, ST, UC and UT, UTF-8 with SpecificCharacterSet ISO_IR 192 (added when absent). Copy limits: 16 MiB of serialized metadata, 1 MiB per value, 65534 bytes per short-VR value in the explicit syntaxes, 6 nested sequence levels, 1024 items per sequence and 10000 elements. Attributes with tags above PixelData are written after the pixels.

Rows and Columns are limited to 1..65535; all samples of all frames count against the existing engine element, evaluation and workspace budgets. In big endian only 16-bit samples are byte-swapped; an odd pixel byte count gets one trailing zero after the last frame. Create-mode serialized metadata is capped at 32 KiB and files at 256 MiB (admission reserves metadata headroom). Input, text projections, metadata and serialized output are admitted before encoding. Encoding (also between frames) and temporary writes poll cancellation. Validation or cancellation observed before atomic rename preserves an existing destination and removes the owned temporary file. Cancellation racing a successful rename cannot undo publication.

Differences from MATLAB: the default transfer syntax is explicit VR little endian (MATLAB: implicit), so the VR default is 'explicit'. Create mode builds only Secondary Capture and drops recognized attributes outside the listed create set; CT and MR objects and IOD checks are not supported yet. Copy mode needs a metadata struct, errors on field names that are not attributes and on option names used as struct fields (MATLAB accepts options as fields), always takes the pixel attributes from X (UseMetadataBitDepths true is not supported yet) and never writes compressed syntaxes (CompressionMode other than 'None' errors, also next to TransferSyntax). A wrong VR, Endian or CreateMode spelling errors even when TransferSyntax overrides the option. Private field names use the MathWorks dicominfo spelling, but this engine's dicominfo returns private fields only when a custom dictionary names them, so a plain round trip carries no private data. A custom session dictionary is read only for private-tag VRs with WritePrivate true; without it, and for every Dictionary option, the call errors as not supported yet; reset dicomdict to factory before writing. dicominfo charges each value 16 times against its 16 MiB budget, so it reads back about 1 MiB of metadata even though copy mode can write more. Multiframe X is always one file (MultiframeSingleFile false is not supported yet) and keeps the given SOPClassUID. Empty X errors instead of writing an empty image. Signed truecolor (int8/int16 R-by-C-by-3) errors as not supported yet. Indexed images, dicomwrite(X,cmap,filename), error as not supported yet. Classes outside the documented int8, int16, uint8 and uint16 (int32, uint32, int64, uint64, single, double, logical) error naming the class. This bounded engine slice does not support palette, compression or status outputs. Native file I/O is unavailable in the browser. This does not complete Engine 7a.

Example

exist('dicomwrite','builtin')   % 5

These are the engine’s help pages. Each page keeps its compatibility notes. File operations are unavailable in the browser.