Next  |  Prev  |  Top  |  JOS Index  |  JOS Pubs  |  JOS Home  |  Search

Communicating with other programs

There are four different ways MATLAB can be used in conjunction with other programs:

      -- USER,
      -- EXEC,
      -- SAVE and LOAD,
      -- MATZ, CALL and RETURN .
Let us illustrate each of these by the following simple example.
      n = 6
      for i = 1:n, for j = 1:n, a(i,j) = abs(i-j);
      A
      X = inv(A)

The example A could be introduced into MATLAB by writing the following Fortran subroutine.

         SUBROUTINE USER(A,M,N,S,T)
         DOUBLE PRECISION A(1),S,T
         N = IDINT(A(1))
         M = N
         DO 10 J = 1, N
         DO 10 I = 1, N
            K = I + (J-1)*M
            A(K) = IABS(I-J)
      10 CONTINUE
         RETURN
         END
This subroutine should be compiled and linked into MATLAB in place of the original version of USER. Then the MATLAB statements
      n = 6
      A = user(n)
      X = inv(A)
do the job.

The example A could be generated by storing the following text in a file named, say, EXAMPLE .

      for i = 1:n, for j = 1:n, a(i,j) = abs(i-j);
Then the MATLAB statements
      n = 6
      exec('EXAMPLE',0)
      X = inv(A)
have the desired effect. The 0 as the optional second parameter of exec indicates that the text in the file should not be printed on the terminal.

The matrices A and X could also be stored in files. Two separate main programs would be involved. The first is:

         PROGRAM MAINA
         DOUBLE PRECISION A(10,10)
         N = 6
         DO 10 J = 1, N
         DO 10 I = 1, N
            A(I,J) = IABS(I-J)
      10 CONTINUE
         OPEN(UNIT=1,FILE='A')
         WRITE(1,101) N,N
     101 FORMAT('A   ',2I4)
         DO 20 J = 1, N
            WRITE(1,102) (A(I,J),I=1,N)
      20 CONTINUE
     102 FORMAT(4Z18)
         END
The OPEN statement may take different forms on different systems. It attaches Fortran logical unit number 1 to the file named A. The FORMAT number 102 may also be system dependent. This particular one is appropriate for hexadecimal computers with an 8 byte double precision floating point word. Check, or modify, MATLAB subroutine SAVLOD.

After this program is executed, enter MATLAB and give the following statements:

      load('A')
      X = inv(A)
      save('X',X)
If all goes according to plan, this will read the matrix A from the file A, invert it, store the inverse in X and then write the matrix X on the file X . The following program can then access X .
         PROGRAM MAINX
         DOUBLE PRECISION X(10,10)
         OPEN(UNIT=1,FILE='X')
         REWIND 1
         READ (1,101) ID,M,N
     101 FORMAT(A4,2I4)
         DO 10 J = 1, N
            READ(1,102) (X(I,J),I=1,M)
      10 CONTINUE
     102 FORMAT(4Z18)
         ...
         ...

The most elaborate mechanism involves using MATLAB as a subroutine within another program. Communication with the MATLAB stack is accomplished using subroutine MATZ which is distributed with MATLAB, but which is not used by MATLAB itself. The preample of MATZ is:

      SUBROUTINE MATZ(A,LDA,M,N,IDA,JOB,IERR)
      INTEGER LDA,M,N,IDA(1),JOB,IERR
      DOUBLE PRECISION A(LDA,N)
C
C     ACCESS MATLAB VARIABLE STACK
C     A IS AN M BY N MATRIX, STORED IN AN ARRAY WITH
C         LEADING DIMENSION LDA.
C     IDA IS THE NAME OF A.
C         IF IDA IS AN INTEGER K LESS THAN 10, THEN THE NAME IS 'A'K
C         OTHERWISE, IDA(1:4) IS FOUR CHARACTERS, FORMAT 4A1.
C     JOB =  0  GET REAL A FROM MATLAB,
C         =  1  PUT REAL A INTO MATLAB,
C         = 10  GET IMAG PART OF A FROM MATLAB,
C         = 11  PUT IMAG PART OF A INTO MATLAB.
C     RETURN WITH NONZERO IERR AFTER MATLAB ERROR MESSAGE.
C
C     USES MATLAB ROUTINES STACKG, STACKP AND ERROR

The preample of subroutine MATLAB is:

      SUBROUTINE MATLAB(INIT)
C     INIT = 0 FOR FIRST ENTRY, NONZERO FOR SUBSEQUENT ENTRIES

To do our example, write the following program:

         DOUBLE PRECISION A(10,10),X(10,10)
         INTEGER IDA(4),IDX(4)
         DATA LDA/10/
         DATA IDA/'A',' ',' ',' '/, IDX/'X',' ',' ',' '/
         CALL MATLAB(0)
         N = 6
         DO 10 J = 1, N
         DO 10 I = 1, N
            A(I,J) = IABS(I-J)
      10 CONTINUE
         CALL MATZ(A,LDA,N,N,IDA,1,IERR)
         IF (IERR .NE. 0) GO TO ...
         CALL MATLAB(1)
         CALL MATZ(X,LDA,N,N,IDX,0,IERR)
         IF (IERR .NE. 0) GO TO ...
         ...
         ...
When this program is executed, the call to MATLAB(0) produces the MATLAB greeting, then waits for input. The command
         return
sends control back to our example program. The matrix A is generated by the program and sent to the stack by the first call to MATZ. The call to MATLAB(1) produces the MATLAB prompt. Then the statements
         X = inv(A)
         return
will invert our matrix, put the result on the stack and go back to our program. The second call to MATZ will retrieve X .

By the way, this matrix X is interesting. Take a look at round(2*(n-1)*X).


Next  |  Prev  |  Top  |  JOS Index  |  JOS Pubs  |  JOS Home  |  Search