Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Forth > Matlab executes...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 3 Topic 3961 of 4050
Post > Topic >>

Matlab executes Forth commands

by mhx@[EMAIL PROTECTED] (Marcel Hendrix) Mar 23, 2008 at 04:33 PM

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




 3 Posts in Topic:
Matlab executes Forth commands
mhx@[EMAIL PROTECTED] (M  2008-03-23 16:33:06 
Re: Matlab executes Forth commands
Albert van der Horst <  2008-03-24 03:10:54 
Re: The Promise of Forth
mhx@[EMAIL PROTECTED] (M  2008-03-24 09:46:04 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Mon May 12 1:00:34 CDT 2008.