Here is small utility to let Matlab execute Forth
commands. It assumes you have the words MAT@[EMAIL PROTECTED]
and MAT!
to access Matlab matrices from your Forth interpreter
(Forth must start Matlab in such a way that both programs
have a console window). The array answer{ (on the Forth
side) is used to quickly get numerical results to Matlab.
I use this to interface LeCroy oscillopes, LTSPICE,
iForth and Matlab to each other. Maybe it will work
from Scilab also (never tried to run LTSPICE under
Linux, so not yet felt the need).
-marcel
-- --------------------------
NEEDS -fsl_util
NEEDS -mlabeng
0 [IF]
Matlab uses the string array FORTHCMD and the flag NEWCMD .
It tests FORTHANSWER for a result when NEWANSWER is not zero.
-- forth.m -----------------------------------------------------
function ans = forth(str)
% Execute the Forth command in the string str.
% Unlock Forth with 'HALT'.
% Note that the four global variables MUST exist because Forth
% tries to access them.
%
% Example: global NEWANSWER FORTHANSWER FORTHCMD NEWCMD
% forth('pi fsqrt answer{ 0 } DF! halt')
% ( prints sqrt(pi) when in Forth WATCH is executed. )
global NEWANSWER FORTHANSWER FORTHCMD NEWCMD
NEWANSWER = 0; FORTHANSWER = 0;
FORTHCMD = str; NEWCMD = 1;
while NEWANSWER == 0
pause(0);
end;
ans = FORTHANSWER;
%EOF
-- ------------------------------------------------------ [THEN]
1 DOUBLE ARRAY answer{
0 VALUE stop?
1 DOUBLE ARRAY newanswer{
DOUBLE DARRAY newcmd{
DOUBLE DARRAY m2f{
CREATE matlabcmd$ 0 , #1024 CHARS ALLOT
: NEW-COMMAND? ( -- bool )
S" NEWCMD" newcmd{ MAT@[EMAIL PROTECTED]
newcmd{ 0 } DF@[EMAIL PROTECTED]
F0<> ;
\ Matlab puts strings in DFLOAT arrays.
\ However, the m2f{ content is 16bit integer characters, where the number
of
\ characters is retrievable with m2f{ CDIM .
: MATLAB-CMD@[EMAIL PROTECTED]
( -- c-addr u )
matlabcmd$ 0!
S" FORTHCMD" m2f{ MAT@[EMAIL PROTECTED]
0 } m2f{ CDIM 0 ?DO B@[EMAIL PROTECTED]
matlabcmd$ @[EMAIL PROTECTED]
+ C! 1 matlabcmd$ +! LOOP
DROP
0e newcmd{ 0 } DF! newcmd{ S" NEWCMD" MAT!
matlabcmd$ @[EMAIL PROTECTED]
;
: HALT ( -- ) TRUE TO stop? ;
\ Evaluation of matlabcmd$ can lead to a new value in answer{
: ANSWER-MATLAB! ( -- )
answer{ S" FORTHANSWER" MAT!
1e newanswer{ 0 } DF!
newanswer{ S" NEWANSWER" MAT! ;
\ Watch for new Matlab commands. HALT stops.
: WATCH ( -- )
CLEAR stop?
BEGIN
NEW-COMMAND? IF MATLAB-CMD@[EMAIL PROTECTED]
EVALUATE
ANSWER-MATLAB!
ELSE #10 MS
ENDIF
stop?
UNTIL ;
: .ABOUT CR ." Try: WATCH -- (Forth) execute commands from the
matlab console."
CR ." forth('ls -l'); -- (Matlab) directory listing in the Forth
window."
CR ." forth('halt'); -- (Matlab) release Forth."
CR ." forth('pi fsqrt answer{ 0 } DF! halt') -- print sqrt(pi) and
stop." ;
CR .ABOUT