%% Set Tag Values
% 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);

% Copyright 2012 The MathWorks, Inc.