# Matlab MEX free-form Fortran: Finally!

**URL:** <https://fortran-lang.discourse.group/t/matlab-mex-free-form-fortran-finally/10874>\
**Category:** Uncategorized\
**Created:** [April 27, 2026, 8:44pm UTC](https://fortran-lang.discourse.group/t/matlab-mex-free-form-fortran-finally/10874 "2026-04-27T20:44:57Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![aledinola](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@aledinola](https://fortran-lang.discourse.group/u/aledinola)\
**Post date:** [April 27, 2026, 8:44pm UTC](https://fortran-lang.discourse.group/t/matlab-mex-free-form-fortran-finally/10874/1 "2026-04-27T20:44:57Z")

</div>

Finally, Matlab R2026a allows mex to compile free-form Fortran code.

From [https://uk.mathworks.com/help/matlab/ref/mex.html](https://uk.mathworks.com/help/matlab/ref/mex.html)

Build MEX functions from free-form Fortran source files

The mex command builds MEX functions from both fixed-form and free-form Fortran (Fortran source file with a .F90 extension) source code.

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [April 27, 2026, 9:06pm UTC](https://fortran-lang.discourse.group/t/matlab-mex-free-form-fortran-finally/10874/2 "2026-04-27T21:06:17Z")

</div>

I have a partial Matlab/Octave to Fortran transpiler [here](https://github.com/Beliavsky/Pure-Fortran/blob/main/xoct2f.py) and have other transpilers to Fortran in the same project, at various levels of maturity.

---

<div class="post-metadata">

**Author:** ![zaikunzhang](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/zaikunzhang/32/785_2.png) [@zaikunzhang](https://fortran-lang.discourse.group/u/zaikunzhang)\
**Post date:** [April 28, 2026, 10:40am UTC](https://fortran-lang.discourse.group/t/matlab-mex-free-form-fortran-finally/10874/3 "2026-04-28T10:40:42Z")

</div>

Great! What a surprise. I have never expected that MathWorks would make any advancement concerning the mexification of Fortran, which has been the [red-haired stepchild](https://www.google.com/search?q=Fortran+MEX+%22red-haired+stepchild%22+&newwindow=1&sca_esv=f8172f1b7064b5e1&biw=1603&bih=915&sxsrf=ANbL-n7q4BKTghVHkyciX5oMm0WjFW5RHg%3A1777372747943&ei=S47wad6XOebP2roP4ueB8Qw&ved=0ahUKEwie6Zi6rZCUAxXmp1YBHeJzIM4Q4dUDCBE&uact=5&oq=Fortran+MEX+%22red-haired+stepchild%22+&gs_lp=Egxnd3Mtd2l6LXNlcnAiI0ZvcnRyYW4gTUVYICJyZWQtaGFpcmVkIHN0ZXBjaGlsZCIgMgUQIRigATIFECEYoAEyBRAhGKABMgUQIRigATIFECEYoAFInQVQsgNYsgNwAngAkAEAmAG9AaABvQGqAQMwLjG4AQPIAQD4AQGYAgOgAtABwgIIEAAY7wUYsAPCAgsQABiJBRiiBBiwA5gDAIgGAZAGApIHAzIuMaAHkwWyBwMwLjG4B8cBwgcFMC4xLjLIBwyACAE&sclient=gws-wiz-serp).

The Fortran backend of [PRIMA](https://github.com/libprima/prima) is in the free form (of course), but it provides MEX interfaces for MATLAB. It achieves this by [a preprocessor](https://github.com/libprima/prima/blob/main/matlab/setup_tools/interform.m) that refactors the free-form code into the [intersection form](https://fortranwiki.org/fortran/show/Continuation%20lines), a form that is both free and fixed. For backward compatibility, it will [continue to do so until we believe that most users are with MATLAB R2026a+](https://github.com/libprima/prima/blob/aa7f8356eb5300dc7420245cf4fbaede0660db02/setup.m#L187-L213).

Another observation: even before R2026a, MATLAB indeed supported the free form (unofficially) on Linux and macOS according to my tests. Windows + MATLAB + MEX = hell for developers, but there are many (really many) users living there, happily.

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [April 28, 2026, 3:14pm UTC](https://fortran-lang.discourse.group/t/matlab-mex-free-form-fortran-finally/10874/4 "2026-04-28T15:14:19Z")

</div>

How does the MEX compilation work exactly — don’t they compile it with a Fortran compiler anyway? So why would it matter if it is in free-form or fixed-form (as long as the Fortran compiler supports it, which I think all do)?

---

<div class="post-metadata">

**Author:** ![aledinola](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@aledinola](https://fortran-lang.discourse.group/u/aledinola)\
**Post date:** [April 28, 2026, 4:18pm UTC](https://fortran-lang.discourse.group/t/matlab-mex-free-form-fortran-finally/10874/5 "2026-04-28T16:18:09Z")

</div>

I’m not sure, but you cannot simply call a Fortran compiler because it would not recognize `mwPointer`.

I have two simple examples on my github page:

> **[GitHub - aledinola/MEX: Matlab - Fortran using MEX](https://github.com/aledinola/MEX)**
>
> Matlab - Fortran using MEX

> <https://github.com/aledinola/MEX/blob/main/mydot_mex.f90>

---

<div class="post-metadata">

**Author:** ![zaikunzhang](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/zaikunzhang/32/785_2.png) [@zaikunzhang](https://fortran-lang.discourse.group/u/zaikunzhang)\
**Post date:** [April 28, 2026, 4:46pm UTC](https://fortran-lang.discourse.group/t/matlab-mex-free-form-fortran-finally/10874/6 "2026-04-28T16:46:47Z")

</div>

> [@certik](#):
>
> So why would it matter if it is in free-form or fixed-form (as long as the Fortran compiler supports it, which I think all do)?

I guess they impose `-fixed` when calling Intel Fortran compiler on Windows. A very strange choice indeed.

---

<div class="post-metadata">

**Author:** ![RonShepard](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@RonShepard](https://fortran-lang.discourse.group/u/RonShepard)\
**Post date:** [April 28, 2026, 5:21pm UTC](https://fortran-lang.discourse.group/t/matlab-mex-free-form-fortran-finally/10874/7 "2026-04-28T17:21:41Z")

</div>

> [@aledinola](#):
>
> I’m not sure, but you cannot simply call a Fortran compiler because it would not recognize `mwPointer`.  
> …
> 
> ```fortran
> mwPointer :: plhs(*), prhs(*)
> 
> ```

In fortran syntax, this would be something like

```auto
type(mwPointer) :: plhs(*), prhs(*)

```

Perhaps this was done in Matlab before `type()` was added to fortran?

---

<div class="post-metadata">

**Author:** ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)\
**Post date:** [April 28, 2026, 6:39pm UTC](https://fortran-lang.discourse.group/t/matlab-mex-free-form-fortran-finally/10874/8 "2026-04-28T18:39:48Z")

</div>

I thought `mwPointer` was just a preprocessor symbol that resolved to `integer(8)` (equal to `integer(c_intptr_t)` in practice).

From a copy of `"fintrf.h"` on GitHub:

```auto
#if defined( __x86_64__ ) || defined(_M_AMD64) || defined(__amd64) || \
    defined(__sparcv9) || defined(__ppc64__)
# define mwpointer integer*8
# define mwPointer integer*8
# define MWPOINTER INTEGER*8
#else
# define mwpointer integer*4
# define mwPointer integer*4
# define MWPOINTER INTEGER*4
#endif

```

(Seems like it was an old copy, before ARM was a common thing; to my surprise, [on Windows on ARM, MATLAB runs via an emulator](https://de.mathworks.com/matlabcentral/answers/2182415-is-matlab-supported-on-windows-on-arm-devices) → [How emulation works on Arm | Microsoft Learn](https://learn.microsoft.com/en-us/windows/arm/apps-on-arm-x86-emulation)).

---

<div class="post-metadata">

**Author:** ![Machalot](https://avatars.discourse-cdn.com/v4/letter/m/ebca7d/32.png) [@Machalot](https://fortran-lang.discourse.group/u/Machalot)\
**Post date:** [April 29, 2026, 7:20am UTC](https://fortran-lang.discourse.group/t/matlab-mex-free-form-fortran-finally/10874/9 "2026-04-29T07:20:15Z")

</div>

This can be overridden, but it is an “advanced user” feature. Puzzling that they took so long with this illogical default.

---

<div class="post-metadata">

**Author:** ![zaikunzhang](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/zaikunzhang/32/785_2.png) [@zaikunzhang](https://fortran-lang.discourse.group/u/zaikunzhang)\
**Post date:** [April 29, 2026, 1:02pm UTC](https://fortran-lang.discourse.group/t/matlab-mex-free-form-fortran-finally/10874/10 "2026-04-29T13:02:25Z")

</div>

You can copy fintrf.h from MATLAB to the same directory as your Fortran code, then you can compile the code using any Fortran compiler, but I have never tried linking.
