5.0

5.0 | 1 rating Rate this file 2 Downloads (last 30 days) File Size: 6.79 KB File ID: #18635

Fortran Logical mex functions

by James Tursa

 

06 Feb 2008 (Updated 06 Feb 2008)

Fortran mex routines for converting logicals to/from MATLAB mxArray variables

| Watch this File

File Information
Description

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.

MATLAB release MATLAB 7.4 (R2007a)
Other requirements Any F90/95 Fortran compiler will probably work, but I have only tested with Intel 9.1 compiler.
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (3)
21 Apr 2009 Thomas Clark

Wow, this just got me out of a very nasty fix (I came to the end of 400 lines of Fortran, then realised I couldn't pass the logical result back!).

Thanks, James, 5*

Although, if I were nit-picking, I probably wouldn't have added the type definitions of mwSize etc - I think to prevent confusion it'd be better to throw an error if they aren't defined - and keep their definitions all in one place (typically the fintrf.h file).

21 Apr 2009 Thomas Clark

Incidentally, I got this to work in Linux (openSUSE10.3 with gcc 4.1.2 compiler). All I needed to do was change the extensions:
testLogicalFunctions.for becomes
testLogicalFunctions.F90
and
mxLogicalFunctions.for becomes
mxLogicalFunctions.f
Why? Something to do with recognising which files are include files and which aren't (there's no standard include in FORTRAN).

15 Aug 2009 James Tursa

Thanks for the 5* rating. The #define for mwSize is needed for older versions of MATLAB that do not have this. That way the same source code can be used with either newer or older versions. I am a bit surprised that the .for extension didn't work ... I had no idea there was a Fortran compiler out there that didn't know what to do with a .for file! But this is good to know. In the future I will use .f extensions (or .f90) for my Fortran submissions.

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
external interface James Tursa 22 Oct 2008 09:46:38
mxcopyptrtological James Tursa 22 Oct 2008 09:46:38
mxcopylogicaltoptr James Tursa 22 Oct 2008 09:46:38
mxcreatelogicalarray James Tursa 22 Oct 2008 09:46:38
mxcreatelog James Tursa 22 Oct 2008 09:46:38

Contact us at files@mathworks.com