      SUBROUTINE MATLAB (INIT)
      IMPLICIT NONE
C
C INIT =0: ORDINARY FIRST ENTRY
C      >0: SUBSEQUENT ENTRIES
C      <0: SILENT INITIALIZATION (SEE MATZ)
C
      INTEGER INIT
C
      INCLUDE MATLAB$KOM:SIZEPARMS.INC
      INCLUDE MATLAB$KOM:VSTK.KOM
      INCLUDE MATLAB$KOM:ALFS.KOM
      INCLUDE MATLAB$KOM:RECU.KOM
      INCLUDE MATLAB$KOM:IOP.KOM
      INCLUDE MATLAB$KOM:COM.KOM
      INCLUDE MATLAB$KOM:MATPLT.KOM
      INCLUDE MATLAB$KOM:SAV.KOM
C
      INCLUDE AMIGA$KOM:INTUIT.INC
C
      INCLUDE DIGLIB$KOM:window.inc
      INCLUDE DIGLIB$KOM:PLTPRM.PRM
      INCLUDE DIGLIB$KOM:GCCLIP.PRM
C         REMOVE GCCOFF FROM INSIDE PLTSIZE.PRM BEFORE INCLUDING HERE
C      INCLUDE DIGLIB$KOM:GCCOFF.PRM
      INCLUDE DIGLIB$KOM:GCCPAR.PRM
      INCLUDE DIGLIB$KOM:GCCPOS.PRM
      INCLUDE DIGLIB$KOM:PLTCOM.PRM
      INCLUDE DIGLIB$KOM:PLTSIZ.PRM
      INCLUDE DIGLIB$KOM:PLTCLP.PRM
      INCLUDE DIGLIB$KOM:GCDCHR.PRM
      INCLUDE DIGLIB$KOM:GCDPRM.PRM
      INCLUDE DIGLIB$KOM:GCDSEL.PRM
      INCLUDE DIGLIB$KOM:GCLTYP.PRM
      INCLUDE DIGLIB$KOM:GCVPOS.PRM
C
      DOUBLE PRECISION S, T
      INTEGER EPS(4), FLOPS(4), EYE(4), RAND(4)
      INTEGER LCOLR1, LCOLR2, LCOLR3, LCOLR4, I
C
C MATLAB CHARACTER SET
C            0       10       20       30       40       50
C
C     0      0        A        K        U   COLON  :  LESS   <
C     1      1        B        L        V   PLUS   +  GREAT  >
C     2      2        C        M        W   MINUS  -
C     3      3        D        N        X   STAR   *
C     4      4        E        O        Y   SLASH  /
C     5      5        F        P        Z   BSLASH \
C     6      6        G        Q  BLANK     EQUAL  =
C     7      7        H        R  LPAREN (  DOT    .
C     8      8        I        S  RPAREN )  COMMA  ,
C     9      9        J        T  SEMI   ;  QUOTE  '
C
      INTEGER ALPHA(52), ALPHB(52)
C
      DATA ALPHA / 1H0, 1H1, 1H2, 1H3, 1H4, 1H5, 1H6, 1H7, 1H8, 1H9,
     .             1HA, 1HB, 1HC, 1HD, 1HE, 1HF, 1HG, 1HH, 1HI, 1HJ,
     .             1HK, 1HL, 1HM, 1HN, 1HO, 1HP, 1HQ, 1HR, 1HS, 1HT,
     .             1HU, 1HV, 1HW, 1HX, 1HY, 1HZ, 1H , 1H(, 1H), 1H;,
     .             1H:, 1H+, 1H-, 1H*, 1H/, 1H\, 1H=, 1H., 1H,, 1H',
     .             1H<, 1H> /
C
C ALTERNATE CHARACTER SET
      DATA ALPHB / 1H0, 1H1, 1H2, 1H3, 1H4, 1H5, 1H6, 1H7, 1H8, 1H9,
     .             1Ha, 1Hb, 1Hc, 1Hd, 1He, 1Hf, 1Hg, 1Hh, 1Hi, 1Hj,
     .             1Hk, 1Hl, 1Hm, 1Hn, 1Ho, 1Hp, 1Hq, 1Hr, 1Hs, 1Ht,
     .             1Hu, 1Hv, 1Hw, 1Hx, 1Hy, 1Hz, 1H , 1H(, 1H), 1H;,
     .             1H|, 1H+, 1H-, 1H*, 1H/, 1H$, 1H=, 1H., 1H,, 1H",
     .             1H[, 1H] /
C
      DATA EPS / 14, 25, 28, 36 /, FLOPS / 15, 21, 24, 25 /
      DATA EYE / 14, 34, 14, 36 /, RAND / 27, 10, 23, 13 /
C
C AMIGA CURSOR COLOR STUFF
      DATA LCOLR1 / Z'9B313B33' /, LCOLR2 / Z'333B3431' /
      DATA LCOLR3 / Z'3B376D00' /, LCOLR4 / Z'9B30306D' /
C
C
C ***      START BY SETTING THE AMIGA PLOT START STATUS TO FALSE
      PLTST = .FALSE.
      BNHERE = .FALSE.
      PLTMAX = 0
C
      IF (INIT.GT.0) GO TO 90
C
C ***      RTE = UNIT NUMBER FOR TERMINAL INPUT
      RTE = 9
      CALL FILES (RTE, BUF)
      RIO = RTE
C
C ***      WTE = UNIT NUMBER FOR TERMINAL OUTPUT
      WTE = 9
      CALL FILES (WTE, BUF)
      WIO = 0
C
      IF (INIT.GE.0) WRITE (WTE, 100) LCOLR1, LCOLR2, LCOLR3, LCOLR4,
     .                                LCOLR1, LCOLR2, LCOLR3, LCOLR4
100   FORMAT (//, 7X, 2A4, A3, '< AMIGA MATLAB >', A4, /,
     .        6X, 2A4, A3, 'Version of 5/01/91', A4)
C
C ***      HIO = UNIT NUMBER FOR HELP FILE
      HIO = 11
      CALL FILES (HIO, BUF)
C
C ***      RANDOM NUMBER SEED
      RAN(1) = 0
C
C ***      INITIAL LINE LIMIT
      LCT(2) = 25
C
      ALFL = 52
      CASE = 0
C
C ***      CASE = 1 for file names in lower case
      DO 20 I = 1, ALFL
        ALFA(I) = ALPHA(I)
        ALFB(I) = ALPHB(I)
20    CONTINUE
C
      VSIZE = VARSIZE
      LSIZE = 48
      PSIZE = 32
      BOT = LSIZE-3
      CALL WSET (5, 0.0D0, 0.0D0, STKR(VSIZE-4), STKI(VSIZE-4), 1)
      CALL PUTID (IDSTK(1,LSIZE-3), EPS)
      LSTK(LSIZE-3) = VSIZE-4
      MSTK(LSIZE-3) = 1
      NSTK(LSIZE-3) = 1
      S = 1.0D0
30    CONTINUE
      S = S/2.0D0
      T = 1.0D0+S
      IF (T.GT.1.0D0) GO TO 30
      STKR(VSIZE-4) = 2.0D0*S
      CALL PUTID (IDSTK(1,LSIZE-2), FLOPS)
      LSTK(LSIZE-2) = VSIZE-3
      MSTK(LSIZE-2) = 1
      NSTK(LSIZE-2) = 2
      CALL PUTID (IDSTK(1,LSIZE-1), EYE)
      LSTK(LSIZE-1) = VSIZE-1
      MSTK(LSIZE-1) = -1
      NSTK(LSIZE-1) = -1
      STKR(VSIZE-1) = 1.0D0
      CALL PUTID (IDSTK(1,LSIZE), RAND)
      LSTK(LSIZE) = VSIZE
      MSTK(LSIZE) = 1
      NSTK(LSIZE) = 1
      FMT = 1
      FLP(1) = 0
      FLP(2) = 0
      DDT = 0
      RAN(2) = 0
      PTZ = 0
      PT = PTZ
      ERR = 0
      IF (INIT.LT.0) RETURN
C
90    CONTINUE
      CALL PARSE
      IF (FUN.EQ.1) CALL MATFN1
      IF (FUN.EQ.2) CALL MATFN2
      IF (FUN.EQ.3) CALL MATFN3
      IF (FUN.EQ.4) CALL MATFN4
      IF (FUN.EQ.5) CALL MATFN5
      IF (FUN.EQ.6) CALL MATFN6
      IF (FUN.EQ.21) CALL MATFN1
      IF (FUN.NE.99) GO TO 90
      RETURN
      END
