Code covered by the BSD License  

Highlights from
mxGetPropertyPtr C-mex function

mxGetPropertyPtr C-mex function

by

 

Fast memory efficient alternative to the API function mxGetProperty.

GetPropertyPtrx(varargin)
% GetPropertyPtrx returns the address of the input variable.
% ************************************************************************************
% 
%  MATLAB (R) is a trademark of The Mathworks (R) Corporation
% 
%  Function:    GetPropertyPtrx
%  Filename:    GetPropertyPtrx.m
%  Programmer:  James Tursa
%  Version:     1.00
%  Date:        March 07, 2011
%  Copyright:   (c) 2011 by James Tursa, All Rights Reserved
% 
%   This code uses the BSD License:
% 
%   Redistribution and use in source and binary forms, with or without 
%   modification, are permitted provided that the following conditions are 
%   met:
% 
%      * Redistributions of source code must retain the above copyright 
%        notice, this list of conditions and the following disclaimer.
%      * Redistributions in binary form must reproduce the above copyright 
%        notice, this list of conditions and the following disclaimer in 
%        the documentation and/or other materials provided with the distribution
%       
%   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
%   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
%   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
%   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
%   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
%   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
%   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
%   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
%   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
%   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
%   POSSIBILITY OF SUCH DAMAGE.
% 
%--------------------------------------------------------------------------
%
%  Building:
% 
%  GetPropertyPtrx is typically self building. That is, the first time you call
%  it the GetPropertyPtrx.m file recognizes that the mex routine needs to be
%  compiled and then the compilation will happen automatically.
% 
%  The usage is as follows (arguments in brackets [ ] are optional):
% 
%  Syntax
% 
%  D = GetPropertyPtrx(A [,B [,C ...]])
% 
%      A, B, C, ... = Any MATLAB variables, including class objects.
%      D = Output array of MATLAB variable structure addresses, one for each input.
% 
%  Description
% 
%  GetPropertyPtrx returns the mxArray structure addresses of the inputs. If the
%  input is a temporary shared-data variable (e.g., the result of a structure,
%  object, or cell expression such as obj(ix).propname or cells{ix}) then the
%  GetPropertyPtrx function searches for the originating object and returns that
%  address instead of the address of the temporary variable. If a non-temporary
%  originating variable cannot be found, then the address of the input is simply
%  returned. The addresses are returned as elements of a UINT64 type variable.
% 
%  Caution: GetPropertyPtrx uses unofficial behind-the-scenes hacking into the
%  mxArray structure of the input variables.  Thus, this is not guaranteed to
%  work for every version of MATLAB. Checks are made in an attempt to ensure
%  that the mxArray structures are as expected, but there is no guarantee that
%  this will work for all versions of MATLAB. If it doesn't work for you, please
%  contact the author. Also, if another process has access to the input(s) or
%  shared data copies of the inputs and changes them while GetPropertyPtrx is
%  running then unpredictable results can occur, such as invalid results or
%  MATLAB crashing.
% 
%  Change Log:
%  2011/Mar/07 --> 1.00, Initial Release
% 
% ***************************************************************************/
 
function varargout = GetPropertyPtrx(varargin)
fname = 'GetPropertyPtrx';
disp(' ');
disp(['Detected that the mex routine for ' fname ' is not yet built.']);
disp('Attempting to do so now ...');
disp(' ');
mname = mfilename('fullpath');
cname = [mname '.c'];
if( isempty(dir(cname)) )
    disp(['Cannot find the file ' fname '.c in the same directory as the']);
    disp(['file ' fname '.m. Please ensure that they are in the same']);
    disp('directory and try again. The following file was not found:');
    disp(' ');
    disp(cname);
    disp(' ');
    error(['Unable to compile ' fname '.c']);
else
    disp(['Found file ' fname '.c in ' cname]);
    disp(' ');
    disp('Now attempting to compile ...');
    disp('(If prompted, please press the Enter key and then select any C/C++');
    disp('compiler that is available, such as lcc.)');
    disp(' ');
    disp(['mex(''' cname ''',''-output'',''',mname,''')']);
    disp(' ');
    try
        mex(cname,'-output',mname);
        disp([ fname ' mex build completed ... you may now use ' fname '.']);
        disp(' ');
    catch
        disp(' ');
        error(['Unable to compile ' fname ' ... Contact author.']);
    end
    [varargout{1:nargout}] = GetPropertyPtrx(varargin{:});
end
end

Contact us