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 6

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 6

"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 6

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 6

"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 6

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 6

"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

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
fortran Thomas Clark 17 Jun, 2009 12:54:04
mex Thomas Clark 17 Jun, 2009 12:54:04
mexprintf Thomas Clark 17 Jun, 2009 12:54:04
format Thomas Clark 17 Jun, 2009 12:54:04
print Thomas Clark 17 Jun, 2009 12:54:04
rssFeed for this Thread

Contact us