tag:www.mathworks.com,2005:/matlabcentral/fileexchange/feedMATLAB Central File Exchangeicon.pnglogo.pngMATLAB Central - File Exchange - type:function category:"Object-Oriented Programming"User-contributed code library2015-01-30T09:38:37-05:00781100tag:www.mathworks.com,2005:FileInfo/320252011-07-01T11:54:15Z2015-01-23T08:22:46ZRead TIFF stacks into Matlab fast, with lazy loadingRead a TIFF stack as a memory-mapped tensor. Handles a large range of internal TIFF formats.<p>See also <a href="http://dylan-muir.com/articles/tiffstack/">http://dylan-muir.com/articles/tiffstack/</a>
<br />If this code is useful to your academic work, please cite the publication in lieu of thanks:
<br />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 (accepted).
<br />Usage: tsStack = TIFFStack(strFilename <, bInvert>)
<br />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.
<br />
<br />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.
<br />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.
<br />Construction:
<br />>> tsStack = TIFFStack('test.tiff'); % Construct a TIFF stack associated with a file
<br />>> tsStack = TIFFStack('test.tiff', true); % Indicate that the image data should be inverted
<br />tsStack =
<br /> TIFFStack handle
<br /> Properties:
<br /> bInvert: 0
<br /> strFilename: [1x9 char]
<br /> sImageInfo: [5x1 struct]
<br /> strDataClass: 'uint16'
<br />Usage:
<br />>> tsStack(:, :, 3); % Retrieve the 3rd frame of the stack, all planes
<br />>> tsStack(:, :, 1, 3); % Retrieve the 3rd plane of the 1st frame
<br />>> size(tsStack) % Find the size of the stack (rows, cols, frames, planes per pixel)
<br />ans =
<br /> 128 128 5 1</p>
<p>>> tsStack(4); % Linear indexing is supported
<br />>> tsStack.bInvert = true; % Turn on data inversion</p>
<p>References:
<br />[1] Francois Nedelec, Thomas Surrey and A.C. Maggs. Physical Review Letters 86: 3192-3195; 2001. DOI: 10.1103/PhysRevLett.86.3192
<br />[2] <a href="http://www.cytosim.org/misc/">http://www.cytosim.org/misc/</a></p>Dylan Muirhttp://www.mathworks.com/matlabcentral/profile/authors/760437-dylan-muirMATLAB 7.11 (R2010b)MATLABfalsetag:www.mathworks.com,2005:FileInfo/296942010-12-13T13:46:06Z2015-01-21T12:19:51ZMappedTensor class - memory-mapped filesA better, transparent memmapfile, with complex number support.<p>See also <a href="http://dylan-muir.com/articles/mapped_tensor/">http://dylan-muir.com/articles/mapped_tensor/</a>
<br />If this function is useful to your academic work, please cite the publication in lieu of thanks:
<br />Muir and Kampa, 2015. "FocusStack and StimServer: A new open source MATLAB toolchain for visual stimulation and analysis of two-photon calcium neuronal imaging data". Frontiers in Neuroinformatics.</p>
<p>This class transparently maps large tensors of arbitrary dimensions to temporary files on disk. Referencing is identical to a standard matlab tensor, so a MappedTensor can be passed into functions without requiring that the function be written specifically to use MappedTensors. This is opposed to memmapfile objects, which cannot be used in such a way. Being able to used MappedTensors as arguments requires that the tensor is indexed inside the function (as opposed to using the object with no indices). This implies that a function using a MappedTensor must not be fully vectorised, but must operate on the mapped tensor in segments inside a for loop.</p>
<p>MappedTensor also offers support for basic operations such as permute and sum, without requiring space for the tensor to be allocated in memory. memmapfile sometimes runs out of virtual addressing space, even if the data is stored only on disk. MappedTensor does not suffer from this problem.</p>
<p>Functions that work on every element of a tensor, with an output the same size as the input tensor, can be applied to a MappedTensor without requiring the entire tensor to be allocated in memory. This is done with the convenience function "SliceFunction".</p>
<p>An existing binary file can also be mapped, similarly to memmapfile. However, memmapfile offers more flexibility in terms of file format. MappedTensors transparently support complex numbers, which is an advantage over memmapfile.</p>
<p>Example:
<br />mtVar = MappedTensor(500, 500, 1000, 'Class', 'single');
<br />% A new tensor is created, 500x500x1000 of class 'single'.
<br />% A temporary file is generated on disk to contain the data for this tensor.</p>
<p>for (i = 1:1000)
<br /> mtVar(:, :, i) = rand(500, 500);
<br /> mtVar(:, :, i) = abs(fft(mtVar(:, :, i)));
<br />end</p>
<p>mtVar = mtVar';</p>
<p>mtVar(3874)</p>
<p>mtVar(:, 1, 1)</p>
<p>mfSum = sum(mtVar, 3);
<br />% The sum is performed without allocating space for mtVar in
<br />% memory.</p>
<p>mtVar2 = SliceFunction(mtVar, @(m)(fft2(m), 3);
<br />% 'fft2' will be applied to each Z-slice of mtVar
<br />% in turn, with the result returned in the newly-created
<br />% MappedTensor mtVar2.</p>
<p>clear mtVar mtVar2
<br />% The temporary files are removed</p>
<p>mtVar = MappedTensor('DataDump.bin', 500, 500, 1000);
<br />% The file 'DataDump.bin' is mapped to mtVar.</p>
<p>SliceFunction(mtVar, @()(randn(500, 500)), 3);
<br />% "Slice assignment" is supported, by using "generator" functions that accept no arguments. The assignment occurs while only allocating space for a single tensor slice in memory.</p>
<p>mtVar = -mtVar;
<br />mtVar = 5 + mtVar;
<br />mtVar = 5 - mtVar;
<br />mtVar = 12 .* mtVar;
<br />mtVar = mtVar / 5;
<br />% Unary and binary mathematical operations are supported, as long as they are performed with a scalar. Multiplication, division and negation take O(1) time; addition and subtraction take O(N) time.</p>Dylan Muirhttp://www.mathworks.com/matlabcentral/profile/authors/760437-dylan-muirMATLAB 7.8 (R2009a)MATLABfalsetag:www.mathworks.com,2005:FileInfo/356902012-03-16T13:44:15Z2015-01-15T07:09:02Zmatlab-ClassInheritanceBrowserA tool for managing and organizing classes, with a simple interface and (optional) tree diagram<p>This is version 0.5.6 of Class Inheritance Browser, originally released @ <a href="http://www.mathworks.com/matlabcentral/fileexchange/27633">http://www.mathworks.com/matlabcentral/fileexchange/27633</a>.
<br />
<br />This tool searches a given directory and populates a browser window with pertinent information about each discovered class, including properties, methods, and superclasses. Other useful features include opening a class definition file from the browser window, opening a file to the line where a certain method is defined, and printing property help to the console.
<br />
<br />Additionally, if the Bioinformatics Toolbox is installed, a tree diagram will be displayed that visualizes inheritance relationships between all Matlab classes found.
<br />
<br />Originally written as a project for Engineering 177, Advanced Matlab Programming, Spring 2010, Professor Andrew Packard, University of California, Berkeley.
<br />You can fork this code @ <a href="https://github.com/kotowicz/matlab-ClassInheritanceBrowser">https://github.com/kotowicz/matlab-ClassInheritanceBrowser</a></p>Andreashttp://www.mathworks.com/matlabcentral/profile/authors/2438379-andreasMATLAB 7.9 (R2009b)MATLABfalsetag:www.mathworks.com,2005:FileInfo/489952015-01-09T20:38:47Z2015-01-09T20:46:43ZStructableEasily convert simple or complex class hierarchies into structs<p>The STRUCTABLE class can be used to convert instantiated MATLAB classes to structs. In practice, this means converting the object's properties into identically-named fields of a struct with "flattened" values. For example, if a property contains a function handle, it will be converted to a string. If a property contains an object and that object inherits STRUCTABLE, it will be converted to struct and saved; otherwise, it will be passed over with a warning message.
<br />STRUCTABLEHIERARCHY uses method overloading to retrieve class-specific information from any inherited classes. In this way, a single call to TOSTRUCT on the instantiated object will ripple through the entire hierarchy to produce a single converted struct.</p>
<p>The primary reason to perform this conversion is to save a "safe" representation of an object into a MAT file. Whereas a struct is a basic MATLAB variable type and struct variables can be loaded without dependency into any MATLAB environment, loading objects in MATLAB requires the defining class to be available on the path. Furthermore, if the class is not identical to the saved object's class definition, MATLAB will generate warnings, or worse.</p>
<p>The reason for using these classes for struct conversion is to provide added functionality where needed. The MATLAB builtin STRUCT method can be used to convert simple objects to structs, but it does not provide enough functionality for complicated class hierarchies.</p>Spencer Kellishttp://www.mathworks.com/matlabcentral/profile/authors/2675823-spencer-kellisMATLAB 8.1 (R2013a)MATLABfalsetag:www.mathworks.com,2005:FileInfo/486372014-12-03T02:04:14Z2014-12-03T02:04:14ZDISPDISP - verbose display of structures and objectsDisplay more information about field and properties of struct or object.<p>DISPDISP(Z), where Z is a struct or object, is the same as calling disp(Z), except for each field or property of Z, the output of that field or property's disp method will be displayed (if it normally only requires one line to do so).
<br />Use dispdisp to overload the disp method for your classes.</p>Sky Sartoriushttp://www.mathworks.com/matlabcentral/profile/authors/5429562-sky-sartoriusMATLAB 8.3 (R2014a)falsetag:www.mathworks.com,2005:FileInfo/179352007-12-08T19:54:14Z2014-11-21T13:55:16ZUIINSPECT - display methods, properties & callbacks of an objectInspect an object handle (Java/COM/HG); display its methods/properties/callbacks in a unified window<p>Syntax:
<br />hFig = uiinspect(obj)
<br />
<br />Description:
<br />UIINSPECT inspects an object handle (e.g., Java, COM, Handle Graphics, Matlab class, Dot-Net etc.) and displays the inspection results in a unified Matlab window. UIINSPECT displays a unified window with all relevant object methods (as can be displayed via Matlab's methodsview function), properties (as can be displayed via Matlab's inspect function), static fields, and callbacks. UIINSPECT also displays properties and fields that are not normally displayed with Matlab's inspect function. Property meta-data such as type, accessibility, visibility and default value are also displayed.
<br />Unlike Matlab's inspect function, multiple UIINSPECT windows can be opened simultaneously.
<br />
<br />Object properties and callbacks may be modified interactively within the UIINSPECT window.
<br />
<br />hFig = UIINSPECT returns a handle to the created figure window. UIINSPECT opens a regular Matlab figure window which may be accessed via hFig (unlike Matlab's methodsview function which opens a Java frame that is not easily accessible from Matlab).
<br />
<br />Examples:
<br />hFig = uiinspect(0); % Matlab root (desktop) handle
<br />hFig = uiinspect(handle(0));
<br />hFig = uiinspect(gcf); % Figure handle
<br />hFig = uiinspect(handle(gcf));
<br />uiinspect(get(gcf,'JavaFrame'));
<br />uiinspect(classhandle(handle(gcf))); % UDD handle
<br />uiinspect(findprop(handle(gcf),'MenuBar')); % Meta-data handle
<br />uiinspect('java.lang.String'); % Java class name
<br />uiinspect(java.lang.String('yes')); % Java class object reference
<br />uiinspect(actxserver('Excel.Application')); % COM/ActiveX object
<br />uiinspect(Employee) % Matlab class object
<br />uiinspect(?handle) % Matlab metaclass object
<br />uiinspect('meta.class') % Matlab class name
<br />uiinspect(System.Diagnostics.Process.GetCurrentProcess) % Dot-Net object
<br />Warning:
<br />This code heavily relies on undocumented and unsupported Matlab functionality. It works on Matlab 7.1 and later releases, but use at your own risk!</p>
<p>Technical explanation and details:
<br /><a href="http://undocumentedmatlab.com/blog/uiinspect">http://undocumentedmatlab.com/blog/uiinspect</a></p>
<p>Bugs and suggestions:
<br />Please send to Yair Altman (altmany at gmail dot com)</p>Yair Altmanhttp://www.mathworks.com/matlabcentral/profile/authors/642467-yair-altmanMATLAB 7.4 (R2007a)14317falsetag:www.mathworks.com,2005:FileInfo/475362014-08-13T21:49:12Z2014-09-01T06:00:58Zcrobarcro/mcode-cpp-utilitiesC++ class wrapper and utilities<p>Header file with C++ class wrapper and other related utilities for creating mex interfaces to C++ objects.
<br />This submission expands on the work done by Oliver Woodford on safely wrapping a persistent C++ object in a classdef wrapper. The supplied header file simplifies the creation of interfaces by providing some preprocessor macros, and a set of utility functions for performing argument checking, conversion between Matlab data types and std data types etc.
<br />A base matlab class (cppinterface.m) is supplied to simplify the creation of your wrapper. This is intended to be subclassed by your classdef interface class.</p>
<p>An example is also provided of allowing two wrapped C++ object to communicate with each other directly through access to their pointers.</p>Richard Crozierhttp://www.mathworks.com/matlabcentral/profile/authors/1590682-richard-crozierMATLAB 8.0 (R2012b)MATLABRequires a C++ compiler38964falsetag:www.mathworks.com,2005:FileInfo/383162012-09-24T15:41:56Z2014-08-26T15:38:20ZObject oriented program demo: animated traffic lights on road intersectionsimple classes and superclass are used to arrange flexible object oriented programming environment<p>Only 31 lines of code in start.m is used to control animated traffic lights on road intersection. LampSuperclass and three clases (trafficLamp, tLamps, map) are used in program. Classes define (1) single Red-Yellow-Green traffic lamp, (2) set of four lamps (North, South, East, West direction) and (3) map of street intersection. Menu is used to change the mode of traffic light operation.</p>Bogumila & Zbigniew MROZEKhttp://www.mathworks.com/matlabcentral/profile/authors/43949-bogumila-zbigniew-mrozekMATLAB 8.0 (R2012b)MATLABtested with MATLAB 2014afalsetag:www.mathworks.com,2005:FileInfo/470252014-06-21T15:01:21Z2014-06-30T22:53:38ZcircVBufcircular vector buffer (constant+fast performance, double buffered)<p>circVBuf class defines a circular double buffered vector buffer
<br />* newest vector has the highest index
<br />* oldest vector has the lowest index
<br />* append adds new vectors at the 'right side' of the buffer
<br />* current buffer always accessable as a complete single subarray buf(fst:lst)
<br />
<br />specials:
<br />* any matrix operations at any time possible !
<br />* time required for appending is independent on buffer status !
<br />* index 'new' indicates the start of last appended vectors
<br />
<br />The main idea of this circular buffer is constant performance (=> double buffering) and avoiding copy operations when using the buffer in a program.
<br />Check the screenshot to see, that this circular buffer has advantages if the buffer is large, but the size of data to append each time is small as the performance of circVBuf does NOT depend on the buffer size, compared to a simple copy buffer.
<br />
<br />The double buffering garanties a predictive time for an append depending on the data to append in any situation. In future this class shall give you a choice for double buffering yes or no - things will speedup, if you do not need the garantied time.</p>
<p> example 1
<br />create a circular vector buffer
<br /> bufferSz = 1000;
<br /> vectorLen= 7;
<br /> cvbuf = circVBuf(int64(bufferSz),int64(vectorLen));</p>
<p> example 2
<br />fill buffer with 99 vectors
<br /> vecs = zeros(99,vectorLen,'double');
<br /> cvbuf.append(vecs);</p>
<p> example 3
<br />loop over lastly appended vectors of a circVBuf:
<br /> new = cvbuf.new;
<br /> lst = cvbuf.lst;
<br /> for ix=new:lst
<br /> vec(:) = cvbuf.raw(:,ix);
<br /> end</p>
<p> example 4
<br />direct array operation on lastly appended vectors in the buffer (no copy => fast)
<br /> new = cvbuf.new;
<br /> lst = cvbuf.lst;
<br /> mean = mean(cvbuf.raw(3:7,new:lst));</p>
<p> example 5
<br />MATLAB makes copies of arrays only when the data referred to changes (this is called copy on write or lazy copying). As long as the first index is fixed, the following should not result in a copy and should be of same speed as example 4 even for large arrays:
<br /> new = cvbuf.new;
<br /> lst = cvbuf.lst;
<br /> vbuf3 = cvbuf.raw(3,new:lst);
<br /> mean(vbuf3);
<br />
<br />For comparison more append-types are added (appendType is argument of constructor):
<br />0: default (double buffering, move fst/lst with each append)
<br />1: simply copy always all -- buf = [buf(2:end,:); vecs(:,:)] (fst == 1 always)
<br />2: copy all into old buffer, if end of buffer reached (fst == 1 always)
<br />3: copy-all into new buffer, if end of buffer reached (fst == 1 always) </p>
<p>Please comment concerning performance and other issues.
<br />Code generation should work.</p>Jens Henrik GĂ¶bberthttp://www.mathworks.com/matlabcentral/profile/authors/5281954-jens-henrik-gobbertMATLAB 8.3 (R2014a)MATLABShould work since MATLAB 2008a as this version introduced the required handle class.falsetag:www.mathworks.com,2005:FileInfo/459412014-03-19T15:45:52Z2014-06-11T06:00:22ZConstructor for function_handlesCreates evaluable function handles for any function, possibly outside the MATLAB search path<p>If you ever found yourself in a situation where you could not or didn't want to add a directory to the MATLAB search path, but still needed speedy access to a function in that directory, this file is for you.
<br />In such cases, having to change directories to gain access to the function is not the best solution: you'd always have to take care to change the current path back to what it was (even on error). Moreover, performance in those cases can be poor if you have to call this function very often.</p>
<p>FUNCTION_HANDLE allows you to create function handles which can successfully be evaluated without loss of performance, even if the function the handle points to is not on the MATLAB search path. </p>
<p>While there often are better ways to accomplish this sort of task (package directories, symbolic links, etc.), there are a few niche cases where these solutions are simply more involved than using this FUNCTION_HANDLE constructor.</p>
<p>Note that FUNCTION_HANDLE overloads a function present in standard MATLAB. This 'native' function is nothing more than documentation (linked to in the help for this constructor) and an error message which says that you cannot use the function to construct handles. As this is exactly what FUNCTION_HANDLE implements, this shadowing is desirable.</p>
<p>Example session: </p>
<p> >> F = function_handle('./path/to/function/myFcn.m')
<br /> F =
<br /> @myFcn</p>
<p> >> A = function_handle(...
<br /> {@cos, '../dir/not/on/path/myFunction.m'})
<br /> A =
<br /> @cos @myFunction</p>
<p> >> A{1}(pi)
<br /> ans =
<br /> -1 </p>
<p> >> functions(A{1})
<br /> ans =
<br /> function: 'min'
<br /> type: 'simple'
<br /> file: ''</p>
<p> >> functions(A{2})
<br /> ans =
<br /> function: 'myFunction'
<br /> type: 'simple'
<br /> file: '/fullpath/dir/not/on/path/myFunction.m'
</p>Rody Oldenhuishttp://www.mathworks.com/matlabcentral/profile/authors/1711536-rody-oldenhuisMATLAB 7.10 (R2010a)false