File Exchange

image thumbnail


version 1.23 (1.47 MB) by

Read a TIFF stack as a memory-mapped tensor. Handles a large range of internal TIFF formats.



See also
If this code is useful to your academic work, please cite the publication in lieu of thanks:
Muir and Kampa, "FocusStack and StimServer: A new open source MATLAB toolchain for visual stimulation and analysis of two-photon calcium neuronal imaging data". Frontiers in Neuroinformatics 2015.

Usage: tsStack = TIFFStack(strFilename <, bInvert>)

A TIFFStack object behaves like a read-only memory mapped TIF file. The entire image stack is treated as a matlab tensor. Each frame of the file must have the same dimensions. Reading the image data is optimised to the extent possible; the header information is only read once.

This class attempts to use the version of tifflib built-in to recent versions of Matlab, if available. Otherwise this class uses a modified version of tiffread [1, 2] to read data. Code is included (but disabled) to use the matlab imread function, but imread returns invalid data for some TIFF formats.
permute, ipermute and transpose are now transparantly supported. Note that to read a pixel, the entire frame containing that pixel is read. So reading a Z-slice of the stack will read in the entire stack.

>> tsStack = TIFFStack('test.tiff'); % Construct a TIFF stack associated with a file
>> tsStack = TIFFStack('test.tiff', true); % Indicate that the image data should be inverted
tsStack =
TIFFStack handle
bInvert: 0
strFilename: [1x9 char]
sImageInfo: [5x1 struct]
strDataClass: 'uint16'
>> tsStack(:, :, 3); % Retrieve the 3rd frame of the stack, all planes
>> tsStack(:, :, 1, 3); % Retrieve the 3rd plane of the 1st frame
>> size(tsStack) % Find the size of the stack (rows, cols, frames, planes per pixel)
ans =
128 128 5 1
>> tsStack(4); % Linear indexing is supported
>> tsStack.bInvert = true; % Turn on data inversion
Support for de-interleaving of channels and slices into the frame axis is also supported (see help text for TIFFStack).

[1] Francois Nedelec, Thomas Surrey and A.C. Maggs. Physical Review Letters 86: 3192-3195; 2001. DOI: 10.1103/PhysRevLett.86.3192

Comments and Ratings (25)


Andi (view profile)

Fantastic tool - strongly recommended!

Dylan Muir

Dylan Muir (view profile)

Hi Ryan, thanks for your feedback.

TIFF files with non-standard tags (there are thousands of them!) will definitely raise warnings. The warnings can be switched off using the matlab `warning` function.

Best regards,

Ryan Muir

Seems to work well, except that I get a flood of warning messages every time I read or access my stack. They all say:

Warning: TIFF library warning - 'TIFFReadDirectory: Unknown field with tag
50288 (0xc470) encountered.'

I used microsoft windows SDK 7.1 as my mex C compiler. I am running windows 7.

Dylan Muir

Dylan Muir (view profile)

Hi Rachel,

I will take a look at the problem to see if I can reproduce it. I have created an issue on github ( to track this; please check there and respond if necessary.

Best regards,


I am running into a problem with TiffStack specifically when I try to load >4GB uint16 tiff stacks that have been saved in imageJ (ie 'big stacks' dealt with by the 'OpenImageJBigStack' fx).

After TiffStack is done running on these faulty-headed tiff files (successfully calling MappedTensor), when I plot frames from the resultant tsStack object, for example using imagesc(tsStack(:,:,1)), the image appears circularly shifted along the 1st dimension, as Avinash mentioned in the comments section of your MappedTensor fx on FileExchange. Unfortunately, the fix mentioned in your response doesn't work for me (I'm assuming the output of >4GB ImageJ tiff stacks that display the error re: headers are binarized, as mentioned elsewhere).

All is working for tiff stacks saved in imageJ that are smaller than this limit.

Any help re: how to trouble shoot would be fantastic - as this fx for my smaller files is a life-savor for setting up efficient parallel processing.


Dylan Muir

Dylan Muir (view profile)

Hi Sandeep,

TIFFStack uses compiled code for some operations, to speed up manipulations of an image stack. This requires the MEX compiler from matlab to be properly configured for your system. TIFFStack will work without these compiled versions, but will be slower. If you would like to use the MEX versions, then you will have to configure your system to successfully compile code.



I get this warning
--- TIFFStack: Compiling MEX functions.
Warning: --- TIFFStack: Could not compile MEX functions. Using
slow matlab versions.
> In TIFFStack>GetMexFunctionHandles (line 1728)
In TIFFStack (line 205)

My Matlab version is (R2015b).

Rob Campbell

Rob Campbell (view profile)

Vijay Iyer

Please disregard my previous rating. I have not used the tool, so cannot rate or comment on it fairly. I was testing something randomly & apparently rating entries get submitted without pressing the submit button. Apologies for the noise! Long live signal.

Vijay Iyer


Xu (view profile)

Hi, I've figured out the problem by repairing my Visual Studio 2013. Thanks at all!


Xu (view profile)

Somehow my problem description got lost. Could you please help?

--- TIFFStack: Compiling MEX functions.
Warning: --- TIFFStack: Could not compile MEX functions. Using slow matlab versions.
> In TIFFStack.TIFFStack>GetMexFunctionHandles at 1703
In TIFFStack.TIFFStack>TIFFStack.TIFFStack at 205
In temp at 29


Xu (view profile)

additional information:

>> ver
MATLAB Version: (R2014b)
MATLAB License Number: 981334
Operating System: Microsoft Windows 7 Professional Version 6.1 (Build 7601: Service Pack 1)
Java Version: Java 1.7.0_11-b21 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode

Hi Dylan,
You are right, the path was not correcly defined.

Now, my questions is how can I use the mex compiler please?
Best regards.

Dylan Muir

Dylan Muir (view profile)

Hi Sébastien,

The warning indicates that matlab can't use the mex compiler, which is required by TIFFStack for accelerated reading.

The error indicates that matlab can't find the file 'tiffread31_header.m', which is the 'private' subdirectory of TIFFStack. The TIFFStack directory needs to be added to the matlab path; perhaps you could check that.

I just downloaded the latest version, and can't reproduce the problem, even if I force TIFFStack to use the non-MEX versions of the access functions.

If you have further problems, please start an issue on github.

Best regards,


I tried your function and I saw this error:

Warning: --- TIFFStack: Using slower non-TiffLib access.
> In TIFFStack (line 187)
Warning: --- TIFFStack: Could not compile MEX functions. Using slow matlab versions.
> In TIFFStack>GetMexFunctionHandles (line 1354)
In TIFFStack (line 192)
Error using TIFFStack (line 390)
*** TIFFStack: Could not open file

Caused by:
Undefined function 'tiffread31_header' for input arguments of type 'char'.

Could you help me please?

Dylan Muir

Dylan Muir (view profile)

Hi HCYU, Please pull the latest version and try again. There was a referencing bug for older versions of matlab -- I think this may have been your issue.
Best regards, Dylan.


HCYU (view profile)

new error message below:

Error using TIFFStack>TS_read_data_Tiff (line 761)
*** TIFFStack: Could not read data from image file.

Error in TIFFStack/subsref (line 438)
tfData = TS_read_data_Tiff(oStack, S.subs, bLinearIndexing);

Error in testvaa3d (line 6)
img = iStack(:, :, i);

Caused by:
Error using tifflib
Directory numbers must be greater than or equal to 1.

Dylan Muir

Dylan Muir (view profile)

Hi HCYU, thanks for your bug report. I have fixed the bug in the latest commit of TIFFStack. You can pull it immediately from github; the file exchange version takes a day to update.
Best regards, Dylan.


HCYU (view profile)

note that: "tfData = oStack.sImageInfo(1).MaxSampleValue - (tfData - oStack.sImageInfo(1).MinSampleValue);"

error info:"Error using -
Integers can only be combined with integers of the same class, or scalar doubles."

Dylan Muir

Dylan Muir (view profile)

Hi David, thanks for your message. I'm sorry about that regression. I've uploaded a new version which fixes the problem.
Best regards, Dylan,


David (view profile)

With the most recent version getting errors when accessing object properties like strDataClass

Error using cellfun
Input #2 expected to be a cell array, was char instead.

Error in TIFFStack/subsref (line 289)
cellfun(@isvalidsubscript, S.subs);

Dylan Muir

Dylan Muir (view profile)

Hi Jana, the class has documentation available if you type `help TIFFStack`. What seems to be the problem?

Can't get this to work. Is there a documentation on how to use it?


Best way, to my knowledge, to work with TIFF stacks on Matlab. Recommended.



Updated description


Updated description


Updated description


TIFFStack now supports opening very long stacks with more than 2^16 frames


Updated description


Updated description


Updated paper citation


Added "neuroscience" tag


Added information that de-interleaving is supported


Reinstated description


Moved TIFFStack to github hosting


Fixed a regression when referencing object properties


Improved referencing for edge cases.


Improved referencing to make it more similar to matlab tensors. Fixed a referencing bug involving a confusion between "58" and ":".


Fixed a bug when using TIFFStack to load an entire stack, with ts(:);


Updated usage notes


Improved efficiency of linear indexing. Removed code to call imread, which was unused in any case.


Added paper reference.


Fixed a bug where tiffflib would fail to read tiled images correctly.


Fixed bugs where tifflib would cause a crash when TIFFStack was not called with a valid filename.


Fixed a bug in indexing into an image file


Updated description


Updated description


Updated description


Dramatic acceleration by using tifflib when possible. Added support for permute and transpose.


Updated description


Updated link to Francois Nedelec


Updated screenshot


Fixed a bug when referencing slice 58 of a stack.


Fixed a bug when indexing non-square TIFF files.

MATLAB Release
MATLAB 7.11 (R2010b)

Inspired: alexludwigklein/MATLAB-Videos

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Win prizes and improve your MATLAB skills

Play today