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@
newcmd{ 0 } DF@ 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@ ( -- c-addr u )
matlabcmd$ 0!
S" FORTHCMD" m2f{ MAT@
m2f{ 0 } m2f{ CDIM 0 ?DO B@+ matlabcmd$ @+ + C! 1 matlabcmd$ +! LOOP DROP
0e newcmd{ 0 } DF! newcmd{ S" NEWCMD" MAT!
matlabcmd$ @+ ;
: 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@ 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
I'm unclear about how the communication works.
Do MAT@ and MAT! access some sort of shared memory between the Forth
and Matlab?
<SNIP>
Groetjes Albert
--
--
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- like all pyramid schemes -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst
> In article <1790393...@frunobulax.edu>, Marcel Hendrix <m...@iae.nl> wrote:
>>Here is small utility to let Matlab execute Forth
>>commands. It assumes you have the words MAT@ 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'm unclear about how the communication works.
> Do MAT@ and MAT! access some sort of shared memory between the Forth
> and Matlab?
That's the way I do it, but you might try DDE (on Windows). I have
a DDE interface to Matlab in iForth, and I think SwiftForth and VFX
have one too. (It's slightly more challenging than box4.)
The interface spec. of these two words is:
S" FORTHCMD" m2f{ MAT@ \ copy FORTHCMD matrix from Matlab to FSL-style Forth dmatrix
newcmd{ S" NEWCMD" MAT! \ copy FSL-style dmatrix to Matlab NEWCMD (must exist)
Of course, you also want S" aap" MEXECUTE (let Matlab execute Forth command),
but I didn't use that in the posting.
-marcel