MATLAB: How to save a series of images (.tiff) as a stack

digital image processingimageimage analysisimage processingMATLAB

I can use imwrite() to save color images as tiff but I would like to have them in a single file like the one attached here. In ImageJ I can see all these images by panning a scrollbar at the bottom. How can I export 5 tiff images into a tiff image stack like this directly from matlab?
Example of a tiff image stack can be downloaded below
Edit:
When I use the Tiff class Tiff('path to file above') I get:
TIFF File: 'C:\blablablablablabla'
Mode: 'r'
Current Image Directory: 1
Number Of Strips: 1
SubFileType: Tiff.SubFileType.Default
Photometric: Tiff.Photometric.RGB
ImageLength: 800
ImageWidth: 800
RowsPerStrip: 800
BitsPerSample: 8
Compression: Tiff.Compression.None
SampleFormat: Tiff.SampleFormat.UInt
SamplesPerPixel: 3
PlanarConfiguration: Tiff.PlanarConfiguration.Chunky
ImageDescription: ImageJ=1.52p
images=5
slices=5
unit=micron
loop=false
Orientation: Tiff.Orientation.TopLeft

Best Answer

t=Tiff('Stacked.tiff','w');
tagstruct.ImageLength = y1; % image height
tagstruct.ImageWidth = x1; % image width
tagstruct.Photometric = Tiff.Photometric.RGB; % https://de.mathworks.com/help/matlab/ref/tiff.html
tagstruct.BitsPerSample = 8;
tagstruct.SamplesPerPixel = 3;
tagstruct.PlanarConfiguration = Tiff.PlanarConfiguration.Chunky; % groups rgb values into a single pixel instead of saving each channel separately for a tiff image
tagstruct.Software = 'MATLAB';
setTag(t,tagstruct)
write(t,squeeze(im2uint8(Imagelayer1)));
writeDirectory(t);
setTag(t,tagstruct)
write(t,squeeze(im2uint8(Imagelayer2))) %%%appends the next layer to the same file t
% do this for as many as you need, or put it in a loop if you can
close(t) %%% this is necessary otherwise you won't be able to open it in imageJ etc to double check, unless you close matlab