In this tutorial, we will:
graph widgetseeval() function to evaluate Tcl scripts from
MathViews scripts.We assume here that the MathXplorer/T distribution is properly installed on
your system. We also assume that your MVPATH environment variable points to the
directory where you installed the standard m-files (such as inv.m). Start your wish executable, and load the Mxt and MxtGr
packages. In Unix, use
% load libmxtgr.so
Under Windows, use the following instead:
% load mxtgr.dll
If the load commands fail, please refer to the installation and configuration page for troubleshooting tips.
Loading the Mxt package makes available
the minterp
command, which we now use for creating a MathViews interpreter called
mv0.
% minterp create mv0
First, let us create a MathViews variable a:
% mv0 eval a = 5
a =
5
To inspect the MathViews variable space, use the MathViews function who:
% mv0 eval who Your variables are : _MVPATH _MVPWD a
We can also enumerate the MathViews variables in a Tcl list by using the
var option:
% mv0 var _MVPATH _MVPWD a
Now, create random 2×3 matrix:
% mv0 eval x = rand(2,3)
x =
0.3020 0.2090 0.3484
0.1040 0.5724 0.1702
Next, create a 3x2 matrix. Note the use of the curly-braces, needed to prevent Tcl from interpreting the contents of square-brackets:
% mv0 eval { y = [ 11 12 ; 21 22 ; 31 32 ] }
y =
11 12
21 22
31 32
Now, We will create a matrix z=x×y and compute its inverse:
% mv0 eval z = x * y
z =
18.5137 19.3732
18.4400 19.2865
% mv0 eval invz = inv(z)
invz =
-109.3487 109.8403
104.5489 -104.9671
Now, lets format invz by using
the var option:
% mv0 var invz
{-109.348692 104.548946} {109.840279 -104.967105}
The var option created a list of two
lists. By default, each sublist represents a column. To group by row, we use
the -r modifier:
% set byrow [ mv0 var invz -r ]
{-109.348692 109.840279} {104.548946 -104.967105}
You can also remove any grouping with -f
(for Flat) modifier. Also, with the -format
option, you can specify a printf-style formatting specification:
% mv0 var invz -f -format %3.1f -109.3 104.5 109.8 -105.0
Next, let us plot some trigonometric data. Our MathViews interpreter
mv0 maintains a set of figures, each one
with a name. It starts out with no figures, so in order to plot, we must first
create one:
% mv0 figure create sin_fig
sin_fig is the name of the figure we
have created. To enumerate the figures associated with mv0, we can use the figure option without arguments:
% mv0 figure sin_fig
As expected, we have only a single figure, the one we just created.
mv0 also designates one of the figures as
the current figure. MathViews plotting functions are directed to the
current figure. If no figures exist, plotting results in a error. To query
mv0 for the current figure, we do:
% mv0 figure current sin_fig
Again, we have only one figure, so mv0
designates it as the current figure by default. We can create a second figure and
designate it as the current figure as follows:
% mv0 figure create tan_fig tan_fig % mv0 figure current tan_fig tan_fig
We think we are ready, so let us create some data and plot it:
% mv0 eval x = [ -pi*0.45 : .1 : pi*0.45 ]';
% mv0 eval y = tan(x);
% mv0 eval plot(x,y)
% mv0 eval title('tan_fig')
So where did the plot go? Since MathViews figures are virtual, no
actual screen rendering takes place in response to plotting
functions. In order to render the figure, we must create a graph Tk widget and subscribe it to a
figure. We loaded the MxtGr
package earlier, making the graph
command available:
% graph .gt -width 250; % pack .gt -padx 2 -pady 2 % .gt configure -figure mv0 tan_fig;
The resulting graph should appear in the top-level wish window like this:
Let us plot sin(x) into the figure sin_fig which we created earlier. If we don't
want to overwrite the contents of tan_fig,
the current figure, we must first set the current figure to sin_fig.
% mv0 figure current sin_fig;
% graph .gs -width 250 -figure mv0 sin_fig -bg #082071c69e79;
% pack forget .gt
% pack .gt .gs -padx 2 -pady 2 -side left
% m eval { plot(x,sin(x)) ; grid ; title('sin_fig') }
The resulting window now looks like this:
Finally, let us give the eeval() a spin.
eeval() is a built-in MathViews function
that takes a string as its only argument. eeval() then uses the Tcl interpreter to evaluate
that string. This allows us to evaluate Tcl scripts from within MathViews
scripts. For example:
% mv0 eval eeval('set greeting "Hi Tcl!"')
ans =
Hi Tcl!
% puts $greeting
Hi Tcl!
Note that eeval() returns the Tcl result
as MathViews array of characters.
eeval() is particularly useful in
conjunction with figure manipulation. A MathViews script that plots onto
several figures can use the eeval()
facility to invoke the Tcl script required for changing the current figure (see
MathXplorer/T demo source). In general, eeval() makes it possible to integrate scripts of
the two languages without breaking their logical flow.
We are done:
% exit
Last modified: Mon Nov 30 10:45:47 PST 1998