Why does Fortran write(6,...) not show in MATLAB window

1 view (last 30 days)
Hi all,
I'm having a bit of an issue with matlab displaying any write to screen commands from my fortran code. (write(6,...) print *)
Right now I have a Fortran Code with a write(6,*)"TOODLES" which will work fine in the compiler, but when I throw a C-Mex wrapper around the fortran code to call it from Simulink, Nothing shows up in the matlab window from the fortran code. printf works from the wrapper .c code, but I get nothing back from Fortran.
I've been looking it up and it seems to have been an issue in later versions of Matlab,
Does Matlab still have this issue? The threads above are out of date, and you would think there would be a fix for it by now.
The problem seems to be that matlab does not understand that the fortran unit=6 for the write function is the standard out
Changing the Fortran Code is not an option entirely. I can add an open statement, but I do not wish to change the write(6,..) to anything else as other programs use this fortran code too. (Thus no mex commands either)
I CAN change the .c-wrapper code. but I dont think that will help
I have Matlab version R2013b with compiler Intel Visual Fortran 13.0 (with Microsoft Visual C++ 2010 linker)
Thanks in advance, Brian

Accepted Answer

James Tursa
James Tursa on 16 Apr 2014
You can look near the end of this thread for a technique that might work for you if you do not need the output interactively within the mex routine itself:
Basically, the technique is to write to a file first, then rewind and print out the file just before exiting the mex routine:
Wrap your Fortran write(6,etc) stuff with this code:
integer*4 mexPrintf, k
character*200 ::str
:
open(unit=6,file='temp.txt',status='unknown')
Then call your write(6,__) statements as-is. Then just before mexFunction returns back to MATLAB, do this:
rewind 6
do
read(6,'(a)',end=500) str
k = mexPrintf(str//achar(10))
enddo
500 close(unit=6)
  7 Comments
Brian
Brian on 17 Apr 2014
Ahh okay, At first I thought you wanted me to go into the BLAH() subroutine and put that above and below the write statements, not wrap the whole subroutine. I miss understood. Thank you
And It works! I just pass the variables through and got it working with just a few tweeks! I did compile the Fortran codes into .obj and then mex'd them with the c-wrapper.
The few things I changed was the :(colon) under character*200 ::str was giving me issues when compiling. I got rid of it and it seemed to work just fine. I don't know why since I'm not too fluent in fortran, but it works.
I also delete the file when closing. close(unit=6, status='delete')
Thank you so much for the help!
James Tursa
James Tursa on 17 Apr 2014
You're welcome. You might also consider trimming the output to get rid of trailing spaces. E.g.
k = mexPrintf(trim(str)//achar(10))

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB Compiler in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!