Tools for ECE Feeding and Milking MATLAB

advertisement
Tools for ECE
Feeding and Milking MATLAB
MATLAB is a powerful and convenient tool for computations. and visualizing results of
computations. The Tools ECE text introduces the MATLAB command language and features.
MATLAB will be especially useful to you if you can readily import problem data from other
tools into MATLAB and export results to other tools. The basic medium for doing this is the
text data file. Text files are also called ASCII files, ASCII being the name of the standard code
for letters, digits and other characters.
Between text files and MATLAB
MATLAB can import one matrix at a time, from a text file. The requirements for this are
spelled out in section 12.2, pp 180, 181 of your text. The file should be given the name of the
MATLAB matrix variable to be loaded, with the extension “.dat”. The text file should only
contain numbers in a format recognized by MATLAB, with the same number of values on every
row, separated by spaces or tabs. MATLAB will create the variable in the current workspace
and load the values from the file, when a load command is typed. The command identifies the
file by its pathname. For example, with data in a file
mymat.dat
on your floppy, the command
load a:\mymat.dat
will create the matrix mymat and populate it with your data. Without the full pathname,
MATLAB expects to find the file in its Work folder.
Similarly, MATLAB results can be written to a text file with the save command, as
described on p. 180. In the save command, the pathname of the file is written if necessary. If a
simple filename is given, the file will be created in the MATLAB Work folder. The “.dat”
extension is used for text files, and the –ascii option should be included. MATLAB allows a
list of variables to be given in the save command, but be cautious with this. If the file is loaded
back into MATLAB, it will come in as a single matrix variable named for the file.
Between text files and the other tools
To transfer data from Word to MATLAB, one could save the Word document as a text file,
but this is generally impractical because the Word document contains material other than
numbers. A numeric table or a section of text containing only numbers in Word can be saved to
the clipboard and then pasted into a new NotePad or WordPad document, which is then saved.
The file name of this document will carry a “.txt” extension, but MATLAB can still read the file.
Since you must provide a pathname, you may want to save the document in the temp folder
which is customarily there for temporary use. For example, the load command in this case would
be
load c:\temp\mydat.txt
(over)
A range in an Excel worksheet displaying only numbers can be copied to the Clipboard and
pasted into a NotePad or WordPad file. Thus it can be loaded into MATLAB as a single matrix.
An entire Excel worksheet containing no text cells can be loaded into MATLAB as a single
matrix, by saving it as a text file. Choose File, SaveAs and select the save type to be “Text (tab
delimited). This produces a text file with the extension “.txt”. In the dialog box you may specify
the folder c:\temp. Alternatively, choose Text (space delimited) and the extension will be
“.prn”. In the latter case, the load command
load c:\temp\mydat.prn
will create and load the matrix mydat in your workspace.
Saving MATLAB data between sessions
The simple save and load commands to save your workspace between sessions is covered
in the text, p.175,6. For saving individual matrices between sessions,, use the binary MAT-files,
not text files. Unless you specify pathnames or change the current directory (cd command, Table
12-1) these files will be saved in the MATLAB Work folder on the hard drive. To save your
current workspace in a file myproject on your floppy, the save command could be
save a:\myproject
and the corresponding load command will restore the workspace. The corresponding
commands are also available on the MATLAB File menu.
Exporting Plots
MATLAB produces plots which can be quite useful in reports and presentations. A plot in
the graphics window can be copied to the Windows ClipBoard by choosing Edit, Copy Picture.
It can be pasted into a Word document from there, or can be saved in a standard picture file
format (.bmp) by pasting it into an MS PaintBrush document and saving in PaintBrush. Another
option is to copy the current screen (PrtScrn key) or the currently active window
(Alt+PrtScrn) to the ClipBoard, and into PaintBrush for editing.
Plots can be saved to be opened later in MATLAB only, by choosing File, SaveAs … in the
graphics window. Windows Office applications do not recognize this type of picture file (.fig),
but these files can be opened later MATLAB sessions by choosing File,Open or typing an open
command of form
open a:\myplot.fig
Download