Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Matlab executes Forth commands

161 views
Skip to first unread message

Marcel Hendrix

unread,
Mar 23, 2008, 10:33:06 AM3/23/08
to
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 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

Albert van der Horst

unread,
Mar 23, 2008, 11:10:54 PM3/23/08
to
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?

<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

Marcel Hendrix

unread,
Mar 24, 2008, 3:46:04 AM3/24/08
to
Albert van der Horst <alb...@spenarnc.xs4all.nl> writes: Re: Matlab executes Forth commands

> 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

Frank

unread,
Oct 28, 2016, 8:11:22 AM10/28/16
to
Hate to relife an old topic but it got my attention. How do you run matlab from inside forth interpreter?

I'm working in win32forth.

JUERGEN

unread,
Oct 28, 2016, 12:04:40 PM10/28/16
to
On Friday, October 28, 2016 at 1:11:22 PM UTC+1, Frank wrote:
> Hate to relife an old topic but it got my attention. How do you run matlab from inside forth interpreter?
>
> I'm working in win32forth.

One way out of many issues might be using VFX SockPuppet,
see http://www.mpeforth.com/sockpuppet/.
Let the C world do what they do well and know about and the Debugging be done by a " Forth Plug In " or to be more undercover call it " Interactive C Plug-In "
0 new messages