Fortran Logical mex functions

Fortran mex routines for converting logicals to/from MATLAB mxArray variables
Updated 6 Feb 2008

No License

mxLogicalFunctions.for provides a suite of functions to support conversion of Fortran logical variables to/from MATLAB style mxArray variables in mex routines or engine applications. Assumes that the MATLAB logical class uses 1 byte for storage of each logical element. When testing a MATLAB logical class variable element for a true value my routines simply look for a non-zero value. When setting a MATLAB logical class element to a true value my routines set the 1-byte MATLAB logical elements to 1. False values are simply set to 0. Instructions for use of each routine are given in the header of each routine. A quick summary is presented below.

subroutine mxCopyPtrToLogical( Ptr, fortran, n )
- Copies MATLAB mxArray logical data to fortran logical variable

subroutine mxCopyLogicalToPtr( fortran, Ptr, n )
- Copies fortran logical variable to MATLAB mxArray logical data

mwPointer function mxCreateLogicalArray( ndim, dims )
- Creates MATLAB mxArray logical array

mwPointer function mxCreateLogicalMatrix( m, n )
- Creates MATLAB mxArray logical matrix

mwPointer function mxCreateLogicalScalar( fortran )
- Creates MATLAB mxArray logical scalar from Fortran logical scalar

logical mxGetLogicalScalar( mxarray )
- Returns Fortran logical scalar from the MATLAB mxArray logical scalar

mwPointer function mxGetLogicals( mxarray )
- Returns the address of the first logical in the mxArray.
Returns 0 if the specified array is not a logical array.

integer*4 mxIsLogicalScalar( mxarray )
- Returns 1 if mxarray is logical class with size 1 x 1
Returns 0 otherwise

integer*4 mxIsLogicalScalarTrue( mxarray )
- Returns 1 if mxarray is logical class with size 1 x 1 and is non-zero
Returns 0 otherwise
(Note: This is very similar to mxGetLogicalScalar)

There are also similar functions specific for logical*1, logical*2, and logical*4 data types supplied with this suite.

Special Note:

Several of the routines use MATLAB supplied functions such as mxGetData and mxCreateNumericMatrix and mxCreateNumericArray with logical class variables. Although the MATLAB doc does not specifically mention that these functions can be used with logical class variables, it does seem to work ok. The only alternative to using these functions would be to call mexCallMATLAB or engCallMATLAB to convert the logical class variables to/from an int8 class. This would slow up the code and force me to make two versions of the code, one for mex files and another for engine applications, so I opted not to do it this way. I use F77 style %VAL constructs instead.

Cite As

James Tursa (2024). Fortran Logical mex functions (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2007a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Find more on Fortran with MATLAB in Help Center and MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes