BUL Computer Tips for Matlab
Questions or suggestions about this page should be sent to mspisar@flip.eecs.umich.edu
Question index:
Q: How do I update from Matlab Version 4.x to 5.x
A: There are a number of changes in the new version of Matlab.
The first thing to do is look at the help provided by Matlab.
Try looking at
How to Update Your Code, one section of the new
Matlab Help Desk.
The Help Desk can be reached from within Matlab by typing
>>helpdesk
Additionally, there is a Perl script to check some of the key changes
in converting your .m files from Version 4 to 5. It is called
/usr/coolstuff/matlab/mat4to5.pl and you can use it as follows:
% /usr/coolstuff/matlab/mat4to5.pl function.m
OR
% /usr/coolstuff/matlab/mat4to5.pl *.m > logfile.txt
Additionally, there are some command line switches to turn off some of the
checks. Use:
-o to turn off looking for ones(A)
-e to turn off looking for eye(A)
-r to turn off looking for rand(A)
-z to turn off looking for zeros(A)
-s to turn off looking for strcmp or strncmp
-i to turn off looking for input(...,'s')
This script was copied from mat4to5.pl and slightly modified.
Other tips include:
- Check for version 5 running with the following Matlab code:
v5=version;
v5=v5(1) > '4';
- Use Version 5 new commands with the 'eval' command. Matlab 4 will give
an error if you use a function name it doesn't recognize, like a new Version 5
command. So by placing the command in an 'eval' statement, you can use the
same .m file for both Version 4 and 5. An example:
eval('colordef none');
- Reset colors of plots to Version 4 defaults by putting the following into your ~/matlab/startup.m file:
v5=version;
v5=v5(1) > '4';
if v5
eval('colordef none');
end
- Add a V4 directory at the top of your path which contains Version 4
commands with the same name as new Version 5 commands. For example, the 'box',
'moveaxis' and 'profile' functions created in the lab have been been
superseded by new Version 5 commands with the same name. By creating a directory
called ~/matlab/V4, putting V4 first in the path, and placing links in this
directory to the Version 4 functions, you can trick Matlab into
running the old commands. To do this:
- Create the directory ~/matlab/V4
- Put the following into your ~/matlab/startup.m file, where <HOME>
is replaced with the full pathname to your home directory:
v5=version;
v5=v5(1) > '4';
if v5
eval('addpath <HOME>/matlab/V4');
end
- Create links to the Version 4 commands in the directory V4, e.g.,
% ln -s /usr/coolstuff/matlab/box.m box.m
Q: How do I import Matlab figures into Microsoft Word on a PC?
A: Do the following:
- Print the figures from Matlab as EPS file using a command like:
print -deps figure1.eps
- Run Word and move the insertion point to the position at
which you want the figure
- Select the Insert..Picture menu option.
- List the "Encapsulated Postscript (*.eps)" type files, and select the
figure you wish to insert.
- To keep the file size down and increase portability, it is recommended
that you select "Link to File" and de-select "Save Picture in Document"
options. This will, however, require that you keep both the document
and all included .eps files in the same place, and when you move or
copy the document you will need to move both the document and all
included .eps files. However, this will make the document portable
between the Mac and Windows versions of Word, and will only increase
the file size slightly, since only a reference to the .eps file is
stored in the document.
An alternative method is to copy the figure from Matlab, and paste it into
Word as a Powerpoint object. The advantage of this is that you will be
able to edit components of the figure such as line styles and font types
and sizes using Powerpoint. To do this, do the following:
- Select Insert..Object from the menu, and select "Create New",
"MS Powerpoint 4.0 Slide", and click on the OK button.
- Once in Powerpoint, resize the slide (using File..Slide Setup) to
12" by 8". (This will create a 6" by 4" figure in Word...don't ask
me why.)
- Delete the title and sub-title from the slide.
- Following the procedure in
How do I import Matlab figures into Powerpoint,
cut the Matlab figure and paste it
into the Powerpoint slide. It will appear blank.
- Drag/resize the figure until it fills the entire area of the slide.
- Double-click on the figure, and answer "Yes" to the prompt to convert
the figure to Powerpoint objects.
- Click on an area away from the figure to de-select the objects.
- Click on a background area of the slide to select what appears to be
the entire slide. Press <Delete> to delete this. At this point all
of the other objects should appear.
- Select the "File..Update and Return" menu item, and answer "Yes" to
the "Update Slide in Document" prompt.
Q: How do I import Matlab figures into Powerpoint on a PC?
A:
You can cut them from Matlab in meta-file form, paste this into
Powerpoint, and convert it to Powerpoint objects.
This is done as follows:
- In Matlab, type "system_dependent(14,'on') before creating the
figure.
- Create the figure, and then maximize the figure window.
- Select the Edit..Copy Options menu item
- Select Windows Metafile as the clipboard format, de-select the
"Honor figure sizes" option, and select the "Invert background" option.
- Select the Edit..Copy menu item. WAIT until you see the cursor flash
and hour glass, or 15 seconds, whichever is shorter. You must wait for
this to give the program time to copy the figure onto the clipboard.
- Switch to Powerpoint, and select the Edit..Paste menu item.
- Re-size the pasted item to fit where you want it.
- Double-click on the pasted item, and convert it to Powerpoint objects.
- Click once outside of the selected area.
- Click once on the black square, and then press <Delete>
to delete the
background object. The figure should now appear properly. You may
select lines and text and change them as you would any Powerpoint
object.
Alternatively, you can print the file in HPGL format, and use the HPGL
import filter in Powerpoint.
This is done as follows:
- Print the figures from Matlab as an HPGL file using a command like:
print -dhpgl figure1.hgl
(note: use the extension .hgl with your file name)
- Run Powerpoint (or Word) and move to the page on which you want to
place the figure
- Select the Insert..Picture menu option.
- List the "HP Graphic Language (*.hgl)" type files, and select the
figure you wish to insert.
- In the options dialog box, de-select "Retain Background" and
"Emulate Line Styles", and use "Normal" color translation. Then
click on the OK button.
- Move and resize the figure to your liking.
- If you want to change the appearance of anything in the figure,
double-click on it, and answer "Yes" to the convert-to-Powerpoint
prompt. You can then select lines and text and modify them as you
would any Powerpoint object.
Q: How do I import Matlab images from a Sun into PowerPoint?
A:
Follow these steps:
- Create your image in the current matlab figure
Note: If you are going to display the image on the Mac or make slides from
the Mac slidemaker, make sure that the image has NO gamma correction
(i.e., a gamma of 1.0). The Mac seems to do its own gamma correction.
- Save it in TIFF format:
>> [x,map] = getframe(gcf);
>> tiffwrite(x,map,<fname>.tiff);
where <fname> is the desired filename.
- Load tiff image into PowerPoint.
Note: if resulting image is not the desired
size, DONT scale it in powerpoint --it will look BAD! Go back to Matlab,
and adjust the 'Position' attributes of the figure to resize it as desired,
then save it again.
Q: How do I import Matlab plots from a Sun into PowerPoint?
A:
There are a number of solutions:
- The best way, which does NOT use a bitmap, and retains each item as an
object, is HPGL format (.hgl extension).
In MATLAB, use
>> print -dhpgl <fname>.hgl;
where <fname> is the desired filename.
When picture is inserted, scale as desired (e.g., 75%), THEN double click
picture to separate into editable objects. You can then change line/text
colors, line types, font size/style, text alignment, ...
Note:
- HPGL is not a standard filter for PowerPoint (Phil had to
download it from Microsoft). We have it on the PC's. It may or may not be
available for the Mac. But since powerpoint documents are easily transferred
from Mac to PC, make your plot slide on the PC, then transfer them to the
Mac for making the scrapbook file (can't be done from PC), unless you are
using EPS output.
- In MATLAB, xlim CANNOT be used, or plot will look bad when objects are
ungrouped in powerpoint. solution: truncate data vector to desired length
before plotting.
- You may have a problem seeing plots with dashed lines, so
make all plot lines solid, and you can change the line type in powerpoint.
In PowerPoint, insert picture, don't keep background, emulate line styles,
use normal color transition.
- A bitmaped (with less resolution) way is to save the plot as a TIFF file. The simplest way is to use the /vol/coolstuff/matlab command fig2tiff,
e.g.,
>> fig2tiff('<fname>.tiff');
where <fname> is the desired filename.
This will save the plot as a black and white image to put into PowerPoint.
See the help on fig2tiff for more details.
Q: What is Tony's Trick?
A:Here's "Tony's trick,"
attributed to Tony Booer of Schlumberger:
To initialize a matrix with values "a" in the first row and values "b"
in the second row, do the following:
[a(1,ones(1,m)) ; b(1,ones(1,m))]
Q: How do I get matab-mode in emacs?
A:Put the following in the .emacs file in your home directory:
;;; Add coolstuff elisp to search path
(if (file-directory-p "/usr/coolstuff/elisp")
(setq load-path (cons "/usr/coolstuff/elisp" load-path))
(message "Could not find /usr/coolstuff/elisp"))
;;; Matlab mode
(autoload 'matlab-mode "matlab" "Enter Matlab-mode." t)
(setq auto-mode-alist (cons '("\\.m$" . matlab-mode) auto-mode-alist))
(defun my-matlab-mode-hook ()
(setq fill-column 76)
(setq matlab-indent-end-before-return t)
(turn-on-auto-fill))
(setq matlab-mode-hook 'my-matlab-mode-hook)
Back to Internal Use Page