      SUBROUTINE STACKG (ID)
      IMPLICIT NONE
C
C GET VARIABLES FROM STORAGE
C
      INTEGER ID(4)
C
      INCLUDE MATLAB$KOM:SIZEPARMS.INC
      INCLUDE MATLAB$KOM:VSTK.KOM
      INCLUDE MATLAB$KOM:IOP.KOM
      INCLUDE MATLAB$KOM:COM.KOM
C
      INTEGER I, J, K, L, L2, L3, LI, LJ, LK, LL, LS, M, MK, MN, MNK, N
C
      LOGICAL EQID
C
C
      IF (DDT.EQ.1) WRITE (WTE, 100) ID
100   FORMAT (' STACKG', 4I4)
      CALL PUTID (IDSTK(1,BOT-1), ID)
      K = LSIZE+1
10    CONTINUE
      K = K-1
      IF (.NOT.EQID (IDSTK(1,K), ID)) GO TO 10
      IF (K.GE.LSIZE-1 .AND. RHS.GT.0) GO TO 98
      IF (K.EQ.BOT-1) GO TO 98
      LK = LSTK(K)
      IF (RHS.EQ.1) GO TO 40
      IF (RHS.EQ.2) GO TO 60
      IF (RHS.GT.2) CALL ERROR (21)
      IF (ERR.GT.0) RETURN
      L = 1
      IF (TOP.GT.0) L = LSTK(TOP)+MSTK(TOP)*NSTK(TOP)
      IF (TOP+1.GE.BOT) CALL ERROR (18)
      IF (ERR.GT.0) RETURN
      TOP = TOP+1
C
C ***      LOAD VARIABLE TO TOP OF STACK
      LSTK(TOP) = L
      MSTK(TOP) = MSTK(K)
      NSTK(TOP) = NSTK(K)
      MN = MSTK(K)*NSTK(K)
      ERR = L+MN-LSTK(BOT)
      IF (ERR.GT.0) CALL ERROR (17)
      IF (ERR.GT.0) RETURN
C
C ***      IF RAND, MATFN6 GENERATES RANDOM NUMBER
      IF (K.EQ.LSIZE) GO TO 97
      CALL WCOPY (MN, STKR(LK), STKI(LK), 1, STKR(L), STKI(L), 1)
      GO TO 99
C
C ***      VECT(ARG)
40    CONTINUE
      IF (MSTK(TOP).EQ.0) GO TO 99
      L = LSTK(TOP)
      MN = MSTK(TOP)*NSTK(TOP)
      MNK = MSTK(K)*NSTK(K)
      IF (MSTK(TOP).LT.0) MN = MNK
      DO 50 I = 1, MN
        LL = L+I-1
        LS = LK+I-1
        IF (MSTK(TOP).GT.0) LS = LK+IDINT (STKR(LL))-1
        IF (LS.LT.LK .OR. LS.GE.LK+MNK) CALL ERROR (21)
        IF (ERR.GT.0) RETURN
        STKR(LL) = STKR(LS)
        STKI(LL) = STKI(LS)
50    CONTINUE
      MSTK(TOP) = 1
      NSTK(TOP) = 1
      IF (MSTK(K).GT.1) MSTK(TOP) = MN
      IF (MSTK(K).EQ.1) NSTK(TOP) = MN
      GO TO 99
C
C ***      MATRIX(ARG,ARG)
60    CONTINUE
      TOP = TOP-1
      L = LSTK(TOP)
      IF (MSTK(TOP+1).EQ.0) MSTK(TOP) = 0
      IF (MSTK(TOP).EQ.0) GO TO 99
      L2 = LSTK(TOP+1)
      M = MSTK(TOP)*NSTK(TOP)
      IF (MSTK(TOP).LT.0) M = MSTK(K)
      N = MSTK(TOP+1)*NSTK(TOP+1)
      IF (MSTK(TOP+1).LT.0) N = NSTK(K)
      L3 = L2+N
      MK = MSTK(K)
      MNK = MSTK(K)*NSTK(K)
      DO 70 J = 1, N
      DO 70 I = 1, M
        LI = L+I-1
        IF (MSTK(TOP).GT.0) LI = L+IDINT (STKR(LI))-1
        LJ = L2+J-1
        IF (MSTK(TOP+1).GT.0) LJ = L2+IDINT (STKR(LJ))-1
        LS = LK+LI-L+(LJ-L2)*MK
        IF (LS.LT.LK .OR. LS.GE.LK+MNK) CALL ERROR (21)
        IF (ERR.GT.0) RETURN
        LL = L3+I-1+(J-1)*M
        STKR(LL) = STKR(LS)
        STKI(LL) = STKI(LS)
70    CONTINUE
      MN = M*N
      CALL WCOPY (MN, STKR(L3), STKI(L3), 1, STKR(L), STKI(L), 1)
      MSTK(TOP) = M
      NSTK(TOP) = N
      GO TO 99
C
97    CONTINUE
      FIN = 7
      FUN = 6
      RETURN
C
98    CONTINUE
      FIN = 0
      RETURN
C
99    CONTINUE
      FIN = -1
      FUN = 0
C
      RETURN
      END
