Math Script Basics
Math Script Basics
qxd
7/2/07
9:04 PM
Page 971
Modern Control Systems, Eleventh Edition, by Richard C Dorf and Robert H. Bishop.
ISBN: 0132270285. 2008 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
APPENDIX
MathScript Basics
B.1 INTRODUCTION
LabVIEW is short for Laboratory Virtual Instrument Engineering Workbench. It is
a flexible graphical development environment from National Instruments, Inc. Engineers and scientists in research, development, production, test, and service industries as diverse as automotive, semiconductor, aerospace, electronics, chemical,
telecommunications, and pharmaceutical use LabVIEW, especially in the area of
testing and measurements, industrial automation, and data analysis. Users of LabVIEW are familiar with the use of the graphical programming language to create
programs relying on graphic symbols to describe programming actions.An important
new development introduced in LabVIEW 8.0 or higher is the MathScript environment. LabVIEW MathScript is a text-based command line environment using m-files
and command line prompts. It is assumed here that the reader has LabVIEW 8 installed and knows how to access the LabVIEW Getting Started window. This appendix only provides an introduction to MathScript. Readers should refer to Learning
with LabVIEW1 for a more complete introduction to LabVIEW and MathScript.
In this appendix, we discuss the MathScript Interactive Window. The essentials
of creating user-defined functions and scripts, of saving and loading data files, and of
using the MathScript Node are presented. With the MathScript Interactive Window,
students will be able to interact with LabVIEW through a command prompt.
971
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a
retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise.
For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
DorfAppBff.qxd
7/2/07
9:04 PM
Page 972
Modern Control Systems, Eleventh Edition, by Richard C Dorf and Robert H. Bishop.
ISBN: 0132270285. 2008 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
972
(b)
( )
FIGURE B.1 Accessing the MathScript Interactive Window from (a) the Getting Started window, or (b) the Tools
pull-down menu on the front panel or block diagram.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a
retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise.
For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
DorfAppBff.qxd
7/2/07
9:04 PM
Page 973
Modern Control Systems, Eleventh Edition, by Richard C Dorf and Robert H. Bishop.
ISBN: 0132270285. 2008 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
973
Variables
Script Editor
Command History
Output
Window
FIGURE B.2
The basic
components of the
MathScript
Interactive Window.
Preview
Pane
Command
Window
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a
retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise.
For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
DorfAppBff.qxd
7/2/07
9:04 PM
Page 974
Modern Control Systems, Eleventh Edition, by Richard C Dorf and Robert H. Bishop.
ISBN: 0132270285. 2008 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
974
Right-click on
Output Window
and select
Clear
Right-click on
Command History
and select
Clear History
FIGURE B.3 Clearing the Command History and the Output Window.
Select y
Define time
interval and
compute
y sin(t)
In pull-down
menu choose
variable type
FIGURE B.4
Showing the data
type in various
formats.
Graph of
the data in
variable y
include: numeric, string, graph, XY graph, sound, surface, and picture. You can edit
a variable in the Preview Pane when the display type is Numeric or String. Selecting
Sound plays the data as a sound, but works for one-dimensional variables only. The
remaining display types show the data as graphs of one sort or another: Graph displays the data on a waveform graph, XY Graph displays the data on an XY graph,
Surface displays the data on a 3D surface graph, and Picture displays the data on an
intensity graph.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a
retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise.
For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
DorfAppBff.qxd
7/2/07
9:04 PM
Page 975
Modern Control Systems, Eleventh Edition, by Richard C Dorf and Robert H. Bishop.
ISBN: 0132270285. 2008 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
975
Open a new MathScript Interactive Window from the Getting Started window
by selecting ToolsMathScript Window, as illustrated in Figure B.1. In the Command Window input the time from t = 0 seconds to t = 10 seconds in increments of
0.1 seconds, as follows:
t = [0 : 0.1 : 10];
Then, compute the y = cos(t)as follows:
y = cos(t);
Notice that in the Variables window the two variables t and y appear, as illustrated in
Figure B.5a. Select the variable y and note that in the Preview Pane the variable appears in the numeric format. Now, in the pull-down menu above the Preview Pane, select Graph.The data will now be shown in graphical form, as illustrated in Figure B.5b.
The graph can be undocked from the Preview Pane for re-sizing and customization.To
undock the graph, right-click on the graph and select Undock Window. The window
can now be re-sized and the plot can be customized interactively and printed.
As an alternative to using the Preview Pane, you can also obtain a plot of the y
versus t programmatically using the plot command:
plot(t,y)
FIGURE B.5 (a) Entering the time, computing y = cos(t) and viewing the variable y in numerical form.
(b) Viewing the variable y in graphical form.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a
retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise.
For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
DorfAppBff.qxd
7/2/07
9:04 PM
Page 976
Modern Control Systems, Eleventh Edition, by Richard C Dorf and Robert H. Bishop.
ISBN: 0132270285. 2008 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
976
Plot window
appears with
y cos(t)
FIGURE B.6
Obtaining a plot of
the cosine function
using the plot
command.
The process is illustrated in Figure B.6. A new window appears that presents the
graph of y versus t. Following the same procedure, see if you can obtain a plot of
y = cos(vt) where v = 4 rad/sec.
help
help classes
help cdt
classes
help class
help
function
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a
retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise.
For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
DorfAppBff.qxd
7/2/07
9:04 PM
Page 977
Modern Control Systems, Eleventh Edition, by Richard C Dorf and Robert H. Bishop.
ISBN: 0132270285. 2008 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
977
Functions of
particular
interest to
students
studying
controls.
FIGURE B.7 Accessing the help for MathScript classes, members, and functions.
class, including abs (i.e., the absolute value), conj (i.e., the complex conjugate function), and exp (i.e., the exponential function). Then, entering help abs in the Command Window will result in an output that contains a description of the abs
function, including examples of its usage and related topics.
B.5 SYNTAX
The syntax associated with MathScript is straightforward. Most students with some
experience programming a text-based language will be comfortable with the programming constructs in MathScript. If you need help getting started with MathScript, you can access help by selecting Help Search the LabVIEW Help from the
MathScript Interactive Window and typing mathscript in the search window.
Eleven basic MathScript syntax guidelines are:
1. Scalar operations: MathScript is ideally suited for quick mathematical operations,
such as addition, subtraction, multiplication, and division. For example, consider the
addition of two scalar numbers, 16 and 3. This is a simple operation that you might
perform on a calculator. This can be accomplished using the MathScript command:
>>16+3
ans=
19
In MathScript, if you perform any calculation or function without assigning the result
to a variable, the default variable ans is used. If you want to assign the value of the
addition of two scalars to the variable x, enter the following command:
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a
retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise.
For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
DorfAppBff.qxd
7/2/07
9:04 PM
Page 978
Modern Control Systems, Eleventh Edition, by Richard C Dorf and Robert H. Bishop.
ISBN: 0132270285. 2008 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
978
>>disp(y);
16
Notice that in the previous example a semicolon was used for the first two lines, and
no output was displayed. In MathScript, if you end a command line with a semicolon,
the MathScript Interactive Window does not display the output for that command.
Some functions display output even if you end the command line with a semicolon.
For example, the disp function displays an output even if followed by a semicolon.
You use the symbol - for subtraction, the symbol / for division, and the symbol
* for multiplication, as illustrated below:
>>16-3
ans=
13
Subtraction
>>16/3
ans=
5.3333
Division
>>16*3
ans=
48
Multiplication
2. Creating matrices and vectors: To create row or column vectors and matrices, use
white space or commas to separate elements, and use semicolons to separate rows.
Consider for example, the matrix A (a column vector),
1
A = C 2 S.
3
In MathScript syntax, you would form the matrix as
A = [1; 2; 3]
Consider for example, the matrix B (a row vector)
B = [1
-2
7].
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a
retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise.
For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
DorfAppBff.qxd
7/2/07
9:04 PM
Page 979
Modern Control Systems, Eleventh Edition, by Richard C Dorf and Robert H. Bishop.
ISBN: 0132270285. 2008 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
979
2
10
0
0
-2 S .
6
10
To create a one-dimensional array equally spaced and incremented by 0.5, use the
MathScript syntax
W t = 1 : 0.5 : 10
t =
1
1.5 2
6 6.5 7
7.5
2.5
8
3
8.5
3.5
9
4
9.5
4.5
10
5.5
4. Accessing individual elements of a vector or matrix: You may want to access specific
elements or subsets of a vector or matrix. Consider the 3 * 3 matrix C:
-1
C = C 4
1
2
10
0
0
-2 S.
6
In MathScript syntax, you can access the element in the second row and third column
of the matrix C, as follows:
-2
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a
retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise.
For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
DorfAppBff.qxd
7/2/07
9:04 PM
Page 980
Modern Control Systems, Eleventh Edition, by Richard C Dorf and Robert H. Bishop.
ISBN: 0132270285. 2008 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
980
>>C(2,:)
ans=
10
-2
In the same way, if you wish to access the entire third column of matrix C, enter the
following command:
>>C(:,3)
ans=
0
-2
6
Suppose you want to extract the 2 * 2 submatrix from C consisting of rows 2 and 3 and
columns 1 and 2. You use brackets to specify groups of rows and columns to access a
subset of data as follows:
A submatrix of C
10
0
5. Calling functions in MathScript: You can call MathScript functions from the Command Window. Consider the creation of a vector of a certain number of elements that
are equally distributed in a given interval. To accomplish this in MathScript syntax, you
can use the built-in function linspace. Using the command help linspace you find that
this function uses the syntax
linspace(a, b, n)
where a specifies the start of the interval, b specifies the end of the interval, and n
identifies the number of elements. Thus, to create a vector of n = 13 numbers equally
distributed between a = 1 and b = 10, use the following command:
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a
retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise.
For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
DorfAppBff.qxd
7/2/07
9:04 PM
Page 981
Modern Control Systems, Eleventh Edition, by Richard C Dorf and Robert H. Bishop.
ISBN: 0132270285. 2008 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
981
8.5
9.25
4.75
5.5
6.25
10
If you do not specify a value for n, the linspace command will automatically return a vector of 100 elements. To select a subset of G that consists of all elements after a specified
index location, you can use the syntax described in guideline 4 and the end function to
specify the end of the vector. For example, the following command will return all elements
of G from the fifth element to the final element:
>>H=G(5: end(G))
H=
4
4.75
5.5
6.25
7.75
8.5
9.25
10
2
10
0
0
1
- 2 S and L = C 0
6
0
0
1
0
0
0 S.
1
To add the two matrices K and L, element by element, enter the following MathScript
command:
>>K+L
ans =
0
2
4
11
1
0
-2
7
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a
retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise.
For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
DorfAppBff.qxd
7/2/07
9:04 PM
Page 982
Modern Control Systems, Eleventh Edition, by Richard C Dorf and Robert H. Bishop.
ISBN: 0132270285. 2008 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
982
2
10
0
-2
Consider the 3 * 1 matrix M (column vector) and the 1 * 3 matrix N (row vector)
1
M = C 2 S and N = [0
3
2].
2
4
6
DorfAppBff.qxd
7/2/07
9:04 PM
Page 983
Modern Control Systems, Eleventh Edition, by Richard C Dorf and Robert H. Bishop.
ISBN: 0132270285. 2008 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
983
r Comments
The script shown above has three comments, all preceded by the % character. In the
next section, we will discuss more details on how to use comments to provide help documentation.
DorfAppBff.qxd
7/2/07
9:04 PM
Page 984
Modern Control Systems, Eleventh Edition, by Richard C Dorf and Robert H. Bishop.
ISBN: 0132270285. 2008 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
984
Grammar
Example
switch
expression
case expression
statement-list
[case
expression
statement-list]
[otherwise
statement-list]
end
switch mode
case start
a = 0;
case end
a = - 1;
otherwise
a = a + 1;
end
When a case in a case-switch statement executes, LabVIEW does not
select the next case automatically.Therefore, you do not need to
use break statements as in C.
For Loop
for expression
statement-list
end
for k = 1 : 10
a = sin(2 * pi * k/10)
end
If-Else
Statement
if expression
statement-list
[elseif
expression
statement-list]
[else
statement-list]
end
if b = = 1
c = 3
else
c = 4
end
Range
start:[step:]end
While Loop
while
expression
statement-list
end
Case-Switch
Statement
DorfAppBff.qxd
7/2/07
9:04 PM
Page 985
Modern Control Systems, Eleventh Edition, by Richard C Dorf and Robert H. Bishop.
ISBN: 0132270285. 2008 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
985
Brief Description
Plots
(2D and 3D)
Standard x-y plot; mesh plot; 3D plot; surface plot; subplots; stairstep plot;
logarithmic plots; stem plot and more.
Approximation
(Curve Fitting & Interpolation)
Cubic spline, cubic Hermite and linear interpolation; exponential, linear and
power fit; rational approximation and others.
Polynomial Operations
Linear Algebra
Matrix Operations
Vector Operations
Cross product; curl and angular velocity; gradient; Kronecker tensor product
and more.
Optimization
Advanced Functions
Basic
Trigonometric
AND, OR, NOT and other logic operations; bitwise shift, bitwise OR and
other bitwise operations.
Data Acquisition/Generation
Other
Programming primitives such as if, for and while loops; unsigned and signed
datatype conversions; file I/O; benchmarking and other timing functions;
various set and string operations and more.
user-defined starlight2 function must be starlight.m. Use unique names for all functions and scripts and save them in a directory that you specified in the Path section of
the FileMathScript Preferences dialog box.
2
The name starlight does not represent a real function. It is used here for illustrative purposes only.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a
retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise.
For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
DorfAppBff.qxd
7/2/07
9:04 PM
Page 986
Modern Control Systems, Eleventh Edition, by Richard C Dorf and Robert H. Bishop.
ISBN: 0132270285. 2008 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
986
User-Defined Functions
MathScript offers more than 500 textual functions for math, signal processing, and
analysis. But what if you have a special purpose function that you want to add to
your personal library? This function may be particular to your area of study or research, and is one that you need to call as part of a larger program. With MathScript
it is simple to create a function once you understand the basic syntax.
A MathScript function definition must use the following syntax:
function outputs = function_name(inputs)
% documentation
script
An example of a user-defined function definition utilizing the proper syntax is
function ave = compute_average(x, y)
% compute_average determines the average of the two inputs x and y.
ave = (x + y)/2;
Begin each function definition with the term function. The outputs lists the output
variables of the function. If the function has more than one output variable, enclose
the variables in square brackets and separate the variables with white space or commas. The function_name is the name of the function you want to define and is the
name that you use when calling the function. The inputs lists the input variables to the
function. Use commas to separate the input variables. The documentation is the set of
comments that you want MathScript to return for the function when you execute the
help command. Comments are preceded with a % character.You can place comments
anywhere in the function; however, LabVIEW returns only the first comment block in
the Output Window to provide the help to the user. All other comment blocks are for
internal documentation. The script defines the executable body of the function.
Checking the help on the function compute_average.m and then executing the
function with x = 2 and y = 4 as inputs yields
help compute_average
compute_average determines the average of the two inputs x and y.
x = 2; y = 4; compute_average(x, y)
ans =
3
Note that there is a MathScript function named mean that can also be used to compute the average of two inputs, as follows:
mean([2 4])
ans =
3
Functions can be edited in the Script Editor window and saved for later use. In Figure
B.8, the buttons Load Script, Save Script As, Save & Compile Script As and Run
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a
retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise.
For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
DorfAppBff.qxd
7/2/07
9:04 PM
Page 987
Modern Control Systems, Eleventh Edition, by Richard C Dorf and Robert H. Bishop.
ISBN: 0132270285. 2008 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
987
Script are shown. You can compile a function when you save it to decrease the runtime compilation time. Selecting Load Script will open a window to browse for the
desired function (or script) to load into MathScript. Similarly, selecting Save Script
As will open a browser to navigate to the desired folder to save the function.
In Figure B.8, the function compute_average is used to compute the average
of two arrays. Notice that the function computes the average element-wise. If
compute_average had inadvertently been named mean, then LabVIEW would execute the user-defined function instead of the built-in function. Generally it is not a
good idea to redefine LabVIEW functions, and students should avoid doing so. If
you define a function with the same name as a built-in MathScript function, LabVIEW executes the function you defined instead of the original MathScript function. When you execute the help command, LabVIEW returns help content for the
function you defined and not the help content for the original MathScript function.
Other examples of valid function syntax for the starlight function include:
function starlight
function a = starlight
function [a b] = starlight
function starlight (g)
function a = starlight (g)
function [a b] = starlight (g)
function starlight (g, h)
function a = starlight (g, h)
function [a b] = starlight (g, h)
There are several restrictions on the use of functions. First, if you define multiple
functions in one MathScript file, all functions following the first are subfunctions
and are accessible only to the main function. A function can call only those functions that you define below it. Second, you cannot call functions recursively. For example, the function starlight cannot call starlight. And third, LabVIEW also does
not allow circular recursive function calls. For example, the function starlight cannot
call the function bar if bar calls starlight.
Scripts
A script is a sequence of MathScript commands that you want to perform to accomplish a task. For convenience and reusability, once you have created a script, you can
save it and load it into another session of LabVIEW at a later time. Also, often you
can use a script designed for a different task as a starting point for the development
of a new script. Since the scripts themselves are saved as common ascii text and editable with any text editor (including the one found in the MathScript Interactive
Window), it is easy to do this. The MathScript functions as well as the user-defined
functions can be employed in scripts.
Continuing the example above, suppose that we used a script to compute the
average of two numbers. The compute_average function could be used within the
script. Once saved, the script can subsequently be loaded into MathScript for use in
another session. A script using the compute_average function is shown in Figure B.9.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a
retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise.
For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
DorfAppBff.qxd
7/2/07
9:04 PM
Page 988
Modern Control Systems, Eleventh Edition, by Richard C Dorf and Robert H. Bishop.
ISBN: 0132270285. 2008 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
988
Script Editor
The function
compute_average.m
Results of
executing script
are displayed
FIGURE B.9 Editing, saving, and running a script to compute the average of two arrays element-wise.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a
retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise.
For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
DorfAppBff.qxd
7/2/07
9:04 PM
Page 989
Modern Control Systems, Eleventh Edition, by Richard C Dorf and Robert H. Bishop.
ISBN: 0132270285. 2008 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
989
method saves the data for all the variables in the workspace, and the second method
allows you to select the variables to save to a file.
To save all the variables in the workspace, select FileSave Data in the MathScript Interactive Window. You also can right-click the Variable List on the Variables window and select Save Data from the shortcut menu. In the file dialog box,
navigate to the directory in which you want to save the data file. Enter a name for
the data file in the File name field and click the OK button to save the data file.
The second method allows you to select the variables to save. In this case, in the
Command Window, enter the command save filename var1, var2, ., varn, where
filename is the name of the file to store the data and var1, var2, varn are the variables that you want to save. In this case, the data will be saved in filename in the
LabVIEW Data directory in the path specified in FileMathScript Preferences .
In Figure B.10 the process of saving data is illustrated. In Figure B.10a, all the variables are saved in the file save_all.mlv after navigating to the folder LabVIEW Data.
In Figure B.10b, the variable x is saved in the file save_x.mlv.
You also can load existing data files into your MathScript session. In the MathScript Interactive Window, select FileLoad Data or right-click the Variable List on
(a)
Location of
saved data
(b)
FIGURE B.10 Saving data files. (a) Saving all the variables in the workspace. (b) Saving select variables.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a
retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise.
For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
DorfAppBff.qxd
7/2/07
9:04 PM
Page 990
Modern Control Systems, Eleventh Edition, by Richard C Dorf and Robert H. Bishop.
ISBN: 0132270285. 2008 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
990
the Variables window and select Load Data from the shortcut menu to load the data
file you want, as illustrated in Figure B.11. Note that you must save data files before
you can load them into the MathScript Interactive Window.
Being able to save scripts is an important feature giving you the capability to develop a library of scripts that you can readily access in future MathScript sessions. To
save a script that you have created in the Script Editor window, select
FileSave Script As, as illustrated in Figure B.12a. You can also save your script by
clicking the Save button on the Script Editor window of the MathScript Interactive
Window, as illustrated in Figure B.12b. In both cases, a file dialog box will appear for
you to navigate to the directory in which you want to save the script. Enter a name
for the script in the File name field. The name must have a lowercase .m extension if
you want LabVIEW to run the script (in this example, we use the name average_
example.m). Click the OK button to save the script.
You can compile a script by selecting FileSave & Compile Script or by clicking
on the Save & Compile Script As button in the MathScript Interactive Window.This will
save and compile the script to decrease the run-time compilation time.You can load existing scripts into the MathScript Interactive Window.This will be useful upon returning
to a MathScript session or if you want to use a script in the current session that was developed in a previous session.To load an existing script, select FileLoad Script or click
the Load button on the Script page on the MathScript Interactive Window.
Figure B.13 illustrates the process of loading scripts. In the example, the script
compute_average.m is loaded into a MathScript session, and then using the Run
Script As button, the script is executed.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a
retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise.
For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
DorfAppBff.qxd
7/2/07
9:04 PM
Page 991
Modern Control Systems, Eleventh Edition, by Richard C Dorf and Robert H. Bishop.
ISBN: 0132270285. 2008 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
991
Section B.7 Saving, Loading, and Exporting Data Files and Scripts
Select Save Script
Navigate to desired folder
Select OK
when done
Name file
to save
script
(a)
(b)
FIGURE B.12 (a) Saving a script using the FileSave Script As pull-down menu. (b) Saving a
script using the Save Script As button on the MathScript Interactive Window.
(a)
Select
Load
Script
Select Load
(b)
FIGURE B.13
(a) Loading a script
using the
FileLoad Script
pull-down menu.
(b) Loading a script
using the Load
Script button on
the MathScript
Select desired file
Interactive Window.
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a
retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise.
For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.
DorfAppBff.qxd
7/2/07
9:04 PM
Page 992
Modern Control Systems, Eleventh Edition, by Richard C Dorf and Robert H. Bishop.
ISBN: 0132270285. 2008 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
992
B.1 Write a script to generate a 3 * 2 matrix M of random numbers using the rand function. Use the help
command for syntax help on the rand function. Verify
that each time you run the script the matrix M changes.
B.2
B.5
B.6
A = C
1
0
-1
-2
1
3 S and B = B
2
5
-1
0
7
R.
-2
This material is protected by Copyright and written permission should be obtained from the publisher prior to any prohibited reproduction, storage in a
retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise.
For information regarding permission(s), write to: Rights and Permissions Department, Pearson Education, Inc., Upper Saddle River, NJ 07458.