Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
FORTRAN mex: print a variable value

Subject: FORTRAN mex: print a variable value

From: Thomas Clark

Date: 17 Jun, 2009 16:51:02

Message: 1 of 9

I'd like a FORTRAN MEX file to print an integer value (from an earlier calculation) to the command line.

In normal fortran, I'd simply use FORMAT and PRINT statements, but of course these are disabled in MATLAB's external interface.

So, I turned to mexPrintf, however, It can't print any numbers (!!!) - all I can do is print a character string - it cannot convert a format string in order to convert between an integer and the character representation of that number.

I can convert between an integer value and it's ASCII conversion character using FORTRAN intrinsics:
call mexPrintf(char(myNumber))
...but the numbers I'm using are out of range of the ASCII set - so I get garbage. Besides, I don't want an ASCII equivalent, I want the actual number to be shown!

Has anyone been able to work around this problem?

Also, it seems like a pretty fundamental thing to miss - is this lack of functionality deliberate (a reason I'm not seeing??)

Thanks all

Tom Clark

Subject: FORTRAN mex: print a variable value

From: James Tursa

Date: 17 Jun, 2009 19:39:01

Message: 2 of 9

"Thomas Clark" <t.clark@remove.spamcantab.net> wrote in message <h1b6tl$eei$1@fred.mathworks.com>...
> I'd like a FORTRAN MEX file to print an integer value (from an earlier calculation) to the command line.
>
> In normal fortran, I'd simply use FORMAT and PRINT statements, but of course these are disabled in MATLAB's external interface.
>
> So, I turned to mexPrintf, however, It can't print any numbers (!!!) - all I can do is print a character string - it cannot convert a format string in order to convert between an integer and the character representation of that number.
>
> I can convert between an integer value and it's ASCII conversion character using FORTRAN intrinsics:
> call mexPrintf(char(myNumber))
> ...but the numbers I'm using are out of range of the ASCII set - so I get garbage. Besides, I don't want an ASCII equivalent, I want the actual number to be shown!
>
> Has anyone been able to work around this problem?
>
> Also, it seems like a pretty fundamental thing to miss - is this lack of functionality deliberate (a reason I'm not seeing??)
>
> Thanks all
>
> Tom Clark

Two solutions:

1) Print to a character string and then print that string using mexPrintf. e.g.

       character*120 line
       integer*4 k
       integer*4 mexPrintf
       integer x
           :
       write(line,*) 'x = ',x
       k=mexPrintf(line//achar(13))

2) Print to a temporary file and then rewind that file and echo it to the screen when the mex file returns. e.g.

      character*120 line
           :
      open(unit=6,file='tmp.txt',status='unknown')
           :
       ! do all of your normal wirte statements, e.g., write(6,__) or write(*,__)
           :
       rewind 6
       ! code to read in unit 6 one line at a time and echo it to the screen
       close(unit-=6)

e.g. see this thread

http://www.mathworks.com/matlabcentral/newsreader/view_thread/243298#625087

James Tursa
      

Subject: FORTRAN mex: print a variable value

From: Thomas Clark

Date: 18 Jun, 2009 08:42:02

Message: 3 of 9

James,

Both solutions great! I used the first and it worked like a charm, so I've been able to find my bug. What a handy hint :)

... turned out that the bug was referencing element -474359598 of an array ;)

Thanks, and kind regards

Tom Clark

Subject: FORTRAN mex: print a variable value

From: Christopher Cassidy

Date: 30 Aug, 2010 22:51:04

Message: 4 of 9

"Thomas Clark" <t.clark@remove.spamcantab.net> wrote in message <h1cukq$ehn$1@fred.mathworks.com>...
> James,
>
> Both solutions great! I used the first and it worked like a charm, so I've been able to find my bug. What a handy hint :)
>
> ... turned out that the bug was referencing element -474359598 of an array ;)
>
> Thanks, and kind regards
>
> Tom Clark

The solutions are workable but there is another way that might be useful with older fortran code that has format statements. As an example,

Old Fortran:
         X=0.5 D0
         Y=0.8 D0
         WRITE (6,5454) X,Y
 8000 FORMAT ('X = ',F8.6,', Y = ',F8.6)

With Matlab Mex files:
         CHARACTER(len=80) :: LINE
         X=0.5 D0
         Y=0.8 D0
         WRITE (LINE,5454) X,Y
         STROUT = mexPrintf(LINE//achar(10))
 8000 FORMAT ('X = ',F8.6,', Y = ',F8.6)

This allowed me to keep the original formating and printing structure with minor changes. Hopefully others will find this useful.

Subject: FORTRAN mex: print a variable value

From: Walter Roberson

Date: 30 Aug, 2010 23:36:09

Message: 5 of 9

On 10-08-30 05:51 PM, Christopher Cassidy wrote:

> Old Fortran:
> X=0.5 D0
> Y=0.8 D0
> WRITE (6,5454) X,Y
> 8000 FORMAT ('X = ',F8.6,', Y = ',F8.6)
>
> With Matlab Mex files:
> CHARACTER(len=80) :: LINE
> X=0.5 D0
> Y=0.8 D0
> WRITE (LINE,5454) X,Y
> STROUT = mexPrintf(LINE//achar(10))
> 8000 FORMAT ('X = ',F8.6,', Y = ',F8.6)

Christopher, I believe that the 8000 before the FORMAT statements should be
5454 for consistency with your WRITE calls.

Subject: FORTRAN mex: print a variable value

From: Edmondo

Date: 17 Jun, 2011 16:59:05

Message: 6 of 9

"James Tursa" wrote in message <h1bgol$b8m$1@fred.mathworks.com>...
> "Thomas Clark" <t.clark@remove.spamcantab.net> wrote in message <h1b6tl$eei$1@fred.mathworks.com>...
> > I'd like a FORTRAN MEX file to print an integer value (from an earlier calculation) to the command line.
> >
> > In normal fortran, I'd simply use FORMAT and PRINT statements, but of course these are disabled in MATLAB's external interface.
> >
> > So, I turned to mexPrintf, however, It can't print any numbers (!!!) - all I can do is print a character string - it cannot convert a format string in order to convert between an integer and the character representation of that number.
> >
> > I can convert between an integer value and it's ASCII conversion character using FORTRAN intrinsics:
> > call mexPrintf(char(myNumber))
> > ...but the numbers I'm using are out of range of the ASCII set - so I get garbage. Besides, I don't want an ASCII equivalent, I want the actual number to be shown!
> >
> > Has anyone been able to work around this problem?
> >
> > Also, it seems like a pretty fundamental thing to miss - is this lack of functionality deliberate (a reason I'm not seeing??)
> >
> > Thanks all
> >
> > Tom Clark
>
> Two solutions:
>
> 1) Print to a character string and then print that string using mexPrintf. e.g.
>
> character*120 line
> integer*4 k
> integer*4 mexPrintf
> integer x
> :
> write(line,*) 'x = ',x
> k=mexPrintf(line//achar(13))
>
> 2) Print to a temporary file and then rewind that file and echo it to the screen when the mex file returns. e.g.
>
> character*120 line
> :
> open(unit=6,file='tmp.txt',status='unknown')
> :
> ! do all of your normal wirte statements, e.g., write(6,__) or write(*,__)
> :
> rewind 6
> ! code to read in unit 6 one line at a time and echo it to the screen
> close(unit-=6)
>
> e.g. see this thread
>
> http://www.mathworks.com/matlabcentral/newsreader/view_thread/243298#625087
>
> James Tursa
>


First method works quite well, but I think the easiest way is to debug without java gui. If you run matlab with "matlab -nodisplay" you are able to see the normal standard output of write(*,*) instructions ... at least this is what happens to me ... on linux machine

Subject: FORTRAN mex: print a variable value

From: Octopode

Date: 7 Aug, 2013 14:14:08

Message: 7 of 9

"Edmondo " <edmondo.minisci@gmail.com> wrote in message <itg14p$l9i$1@newscl01ah.mathworks.com>...
> "James Tursa" wrote in message <h1bgol$b8m$1@fred.mathworks.com>...
> > "Thomas Clark" <t.clark@remove.spamcantab.net> wrote in message <h1b6tl$eei$1@fred.mathworks.com>...
> > > I'd like a FORTRAN MEX file to print an integer value (from an earlier calculation) to the command line.
> > >
> > > In normal fortran, I'd simply use FORMAT and PRINT statements, but of course these are disabled in MATLAB's external interface.
> > >
> > > So, I turned to mexPrintf, however, It can't print any numbers (!!!) - all I can do is print a character string - it cannot convert a format string in order to convert between an integer and the character representation of that number.
> > >
> > > I can convert between an integer value and it's ASCII conversion character using FORTRAN intrinsics:
> > > call mexPrintf(char(myNumber))
> > > ...but the numbers I'm using are out of range of the ASCII set - so I get garbage. Besides, I don't want an ASCII equivalent, I want the actual number to be shown!
> > >
> > > Has anyone been able to work around this problem?
> > >
> > > Also, it seems like a pretty fundamental thing to miss - is this lack of functionality deliberate (a reason I'm not seeing??)
> > >
> > > Thanks all
> > >
> > > Tom Clark
> >
> > Two solutions:
> >
> > 1) Print to a character string and then print that string using mexPrintf. e.g.
> >
> > character*120 line
> > integer*4 k
> > integer*4 mexPrintf
> > integer x
> > :
> > write(line,*) 'x = ',x
> > k=mexPrintf(line//achar(13))

> First method works quite well, but I think the easiest way is to debug without java gui. If you run matlab with "matlab -nodisplay" you are able to see the normal standard output of write(*,*) instructions ... at least this is what happens to me ... on linux machine


Hello,

I had the same question and tried solution 1) above using the write function, in R2013a in OS X 10.8.
I tried with both gui and non-gui (from terminal, with -nodesktop).

I got this :

Symbol not found: __gfortran_transfer_character_write


Is the "write" command not allowed anymore in MEX ?

Thanks

Subject: FORTRAN mex: print a variable value

From: Octopode

Date: 7 Aug, 2013 18:17:11

Message: 8 of 9

> Hello,
>
> I had the same question and tried solution 1) above using the write function, in R2013a in OS X 10.8.
> I tried with both gui and non-gui (from terminal, with -nodesktop).
>
> I got this :
>
> Symbol not found: __gfortran_transfer_character_write
>
>
> Is the "write" command not allowed anymore in MEX ?
>
> Thanks

I found a solution to my problem. Here it is in case someone encounters it.
 This thread gave me the correct hint :
http://www.mathworks.com/matlabcentral/newsreader/view_thread/309061

Although in that thread a hard link is used. I'm using a symbolic link as it's the only kind of link i'm used to.
So, had go to the matlab subdirectory :
/Applications/MATLAB_R2013a.app/sys/os/maci64

and renamed the file (just to save it)
mv libgfortran.3.dylib libgfortran.3.dylib.old

I created the symbolic link
ln -s /usr/local/lib/libgfortran.3.dylib libgfortran.3.dylib

where this linked libgfortran.3.dylib is from my own gfortran installation, version 4.8.1, downloaded from the hpc packages :
http://freefr.dl.sourceforge.net/project/hpc/hpc/gcc/gcc-mlion.tar.gz, http://sourceforge.net/projects/hpc/files/hpc/gcc/gcc-mlion.tar.gz/download?use_mirror=freefr&download=

And voilĂ  ! No error on the write command.

Subject: FORTRAN mex: print a variable value

From: Pablo

Date: 22 Jun, 2014 21:06:13

Message: 9 of 9

Hi,

Did you have to do something else for this to work? I keep getting the same error after creating the symbolic link.

I did recompile the code after this modification, am I missing something? I have gcc-4.8.1

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us