Main Content

setTag

Set value of tag

Description

setTag(t,tagID,tagValue) sets the value of the TIFF tag specified by tagID to the value specified by tagValue in the TIFF file associated with the tiff object t.

example

setTag(t,tagStruct) sets all the tags specified in tagStruct. The tagStruct structure can contain multiple tag names and their corresponding values.

example

Examples

collapse all

You can set tags for a TIFF file by specifying the tag name, the tag numeric identifier, or by specifying a structure of multiple tag names and values.

Create a new TIFF file, write image data, and then set tag values in different ways.

Read sample data into an array, imdata. Create a Tiff object associated with a new file, myfile.tif, and open the file for writing.

imdata = imread('example.tif');
t = Tiff('myfile.tif','w');

Set tag values by specifying the tag name.

setTag(t,'Photometric',Tiff.Photometric.RGB)
setTag(t,'PlanarConfiguration',Tiff.PlanarConfiguration.Chunky)

Set tag values by specifying the numeric tag identifier. Use the fields of the Tiff.TagID structure to obtain the tag IDs. For instance, the Tiff.TagID.ImageLength contains the numeric identifier for the ImageLength tag.

setTag(t,Tiff.TagID.ImageLength,size(imdata,1))
setTag(t,Tiff.TagID.ImageWidth,size(imdata,2))

Create a structure with fields named after TIFF tags and assign values to the fields. Pass this structure to the setTag method to set the values of these tags.

tagStruct.BitsPerSample = 8;
tagStruct.SamplesPerPixel = 3;
tagStruct.TileWidth = 128;
tagStruct.TileLength = 128;
tagStruct.Compression = Tiff.Compression.JPEG;
tagStruct.Software = 'MATLAB';
setTag(t,tagStruct)

Write the image data to the TIFF file and close the Tiff object.

write(t,imdata);
close(t);

Input Arguments

collapse all

Tiff object representing a TIFF file. Use the Tiff function to create the object.

Tag ID of a Tiff object, specified as a character vector or string scalar, or a numeric identifier.

For example, you can specify tagId for the ImageWidth tag as any of these:

  • Character vector or string scalar containing the tag name 'ImageWidth'

  • Numeric identifier 256 for the ImageWidth tag defined by the TIFF specification

  • Field of the Tiff.TagID structure Tiff.TagID.ImageWidth.

The names of the fields of the Tiff.TagID structure are valid tag names that contain the corresponding tag numeric identifiers. For instance, the field Tiff.TagID.ImageWidth contains the value 256. To see a list of all the tags along with their numeric identifiers, type Tiff.TagID in the command window.

Example: 'ImageWidth'

Example: 256

Example: Tiff.TagID.ImageWidth

Data Types: double | char | string

Tag value, specified as a valid numeric identifier, or a character vector or string scalar.

For example, you can specify tagValue for the PlanarConfiguration tag in one of these ways:

  • Numeric identifier 2 that sets the value of the PlanarConfiguration tag indicating a separate configuration

  • Field of the Tiff.PlanarConfiguration structure Tiff.PlanarConfiguration.Separate

The Tiff.PlanarConfiguration structure contains the numeric identifiers for all the valid values for the PlanarConfiguration tag. To see all the valid tag values for any given tag, type Tiff.TagName in the command window. For example, to see all the valid values for the Photometric tag, type Tiff.Photometric in the command window.

Data Types: double | char | string

Multiple tags, specified as a structure containing tag names and their corresponding values. The names of fields in tagstruct must be the name of supported TIFF tags, and their corresponding values must be valid TIFF tag values.

Data Types: struct

Tips

  • If you are modifying a tag rather than creating it, then you must use the rewriteDirectory method after using the setTag method.

Algorithms

collapse all

References

This function corresponds to the TIFFSetField function in the LibTIFF C API. To use this function, you must be familiar with the TIFF specification and technical notes. View this documentation at LibTIFF - TIFF Library and Utilities.

Version History

Introduced in R2009b