Upgrading from MATLAB 4 to MATLAB 5.0     Search    Help Desk 

Converting M-Files to MATLAB 5.0

This section describes some changes you can make to your code to eliminate error messages and warnings due to incompatible and noncompliant statements.

Table 2-1: Language Changes  
Function
Change
Action
auread, auwrite
New syntax.
Change call to use new syntax.
bessel
functions
The bessel functions no longer produce a table for vector arguments of the same orientation.
For example, in besselj(nu,x), specify nu as a row and x as a column to produce a table.
case,
otherwise,
switch
case, otherwise, and switch cannot be used as variable names.
Rename your variables.
dialog
dialog.m now creates a modal dialog.
Use the msgbox function instead.
echo
echo does not display multiline matrices.
Update code.
end
extra end statements.
Remove redundant end statements.
eps
eps is a function. eps = 0 no longer redefines eps for other functions (it makes a local variable called eps in the current workspace). Functions that base their tolerance on an externally defined eps won't work.
Change code accordingly.
for
for loop variable different after loop for empty loops. In
MATLAB 4:
i = 10;
for i = 1:0, %goes nowhere
end
i
produces i = 10.
In MATLAB 5.0 it produces
i = [].
Protect the for loop with an isempty call:
i = 10;
if ~isempty(n)
 for i=1:n
 end
end
i

global
Undefined globals
Define globals before they are used. Always put the global statement at the top of the M-file (just below the help comments).
MATLAB 4 produced a link in the workspace to an uninitialized global. It shows up in whos but exist returns 0. Do not use exist to test for the first time the global has been accessed. Use isempty.
gradient
gradient no longer produces complex output.
Use two outputs in the 2-D case.
input
input('prompt','s') no longer outputs an initial line feed. Prompts now show up on the same line.
Update code accordingly if this causes a display problem. Add \n in the prompt string to force a line feed.
interp1
The old interp1 syntax (interp1(x,n)) no longer calls interpft. A warning was in place in MATLAB 4.
Update code accordingly.

interp1 now returns a row vector when given a row vector. It used to return a column vector.
Transpose the output of interp1 to produce the MATLAB 4 result when xi is a row vector.

interp1('spline') returns NaN's for out of range values.
Use spline directly.
interp2
The old interp2 syntax (interp2(x,y,xi)) no longer calls interp1. A warning was in place in MATLAB 4.
Update code accordingly.
interp3
The old interp3 syntax (interp3(z,m,n) or interp3(x,y,z,xi,yi)) no longer calls griddata. A warning was in place in MATLAB 4. interp3 is now 3-D interpolation.
Update code accordingly.
Automeshing
interpolation
commands
Interpolation automeshing has changed. griddata, interp2, interp3, interpn, and bessel* now automesh if (xi,yi) or (nu,z) are vectors of different orientations. Previously they automeshed if the vectors were different size.
When xi and yi are vectors of the same orientation but different lengths, change calls such as interp2(...,xi,yi) to interp2(...,xi,yi').
isempty
A == [] and A ~= [] as a check for an empty matrix produce warning messages.
Use isempty(A) or ~isempty(A). In a future version A == [] will produce an empty result.
isspace
isspace only returns true (1) on strings. isspace(32) is 0 (it was 1 in MATLAB 4).
Wrap your calls to isspace with char.
logical

Some masking operations where the mask isn't defined using a logical expression now produce an out of range index error.
Wrap the subscript with a call to
logical or use the logical expression A~=0 to produce MATLAB 4 behavior.

Boolean indexing is no longer directly supported.
Use logical to create the index array.
matlabrc
On the PC, MATLAB no longer stores the path in matlabrc.
MATLAB for PC and UNIX now uses pathdef.m.
max
max(size(v)), as a means to determine the number of elements in a vector v, fails when v is empty.
max ignores NaNs.
Use length(v) in place of max(size(v)).
min
min ignores NaNs.
Change code if necessary.
nargin, nargout
nargin and nargout are functions.
nargout = nargout-1 (and any similar construction) is an error. To work around this change, assign nargin to a local variable and increment that variable. Rename all occurrences of nargin to the new variable. The same holds true for all functions.
ones
A(ones(size(A))) no longer produces A.

This statement produces copies of the first element of A. Use A(ones(size(A))~=0) or just A to produce the MATLAB 4 behavior.

No longer accepts column vector.

Size vector must be a row vector with integer elements.

Functions such as ones, eye, rand, and zeros give an error if supplied with a matrix argument (such as zeros(A)).
Use the syntax ones(size(A)) instead.
polyfit
Second output now a structure.
Change code to access structure component.
print
-ocmyk is now -cmyk.
-psdefcset is now -adobecset.
GIF format no longer supported.
Texture mapped surfaces do not print with painter's algorithm.
Update code accordingly.
Update code accordingly.
Use alternate format.
Use -zbuffer.
rand
rand('normal') and
rand('uniform')
no longer supported.
Use randn for normally distributed and rand for uniformly distributed random numbers.
round
Subscripts must be integers.
To reproduce MATLAB 4 behavior, wrap noninteger subscripts with round(). Strings are no longer valid subscripts (since they are not integers in the strict sense).
slice
slice no longer requires the number of columns (ncols) argument.
Update code accordingly.
sound
Doesn't autoscale.
Use soundsc.
strcmp
strncmp

strcmp and strncmp now return false (0) when any argument is numeric. They used to perform an isequal.
Call isequal for all nonstrings you want to compare.
wavread, wavwrite
New syntax.
Change call to use new syntax.
zeros
No longer accepts column vector.
Size vector must be a row vector with integer elements.
Note: The following language changes do not directly apply to specific functions.

a(:) = b where a doesn't exist creates an error. This used to do the same thing as a = b(:) when a didn't exist.
Either initialize a or use a = b(:) instead.

Must use an explicitly empty matrix to delete elements of an array, as in a(i) = [] or
a(i,:) = []. This syntax works for all built-in data types (including cell arrays and structures).
Change code accordingly.

The syntax a(i) = B, when B is empty, no longer deletes elements.
Use a(i) = [] instead.

An attempt to delete elements of an array outside its range is no longer (incorrectly) ignored. An error is generated.
Change code accordingly.

Undefined variables.
To reproduce MATLAB 4 behavior, initialize your variable to the empty matrix ([]) or empty string ('').

Undefined outputs.
To reproduce MATLAB 4 behavior, initialize your outputs to the empty matrix ([]).

Indices must be integers. Strings are no longer valid indices.
Use a(round(ind)) to get MATLAB 4 behavior.

_,^,{, and } are now interpreted, not displayed.
Use \_,\^, \{, and\}.

Concatenating a string and a double truncates the double.
Use double to convert the string before concatenating.

Input arguments are no longer evaluated left to right.
Evaluate input arguments before passing them to a function.

String handling difference.
In MATLAB 4
a = 32*ones(1,10);
a(1:5) = 'hello'
produces 'hello'.
In MATLAB 5.1, it produces:
104 101 108 11 32 32 32 32 32.
Initialize a to be a character array or convert it after assignment.

Using inline matrix constants and continued matrix constants inside function calls:
fun(arg1,[1 2 3
3 4 5
,
5 6 6])
is a syntax error.
Put continuation dots and semicolon after each matrix line.
Table 2-1: Obsolete Language Functions  
Obsolete Function
Action
casesen
Remove the call.
csvread, csvwrite
Use dlmread(filename,',') and dlmwrite(filename,',').
ellipk
Replace with ellipke.
extent
Replaced by Extent property.
figflag
Use findobj.
finite
Rename to isfinite. finite was removed in Release 11
(MATLAB 5.3).
fwhich
Use which.
hthelp
hthelp works in Release 11 (MATLAB 5.3), but will not be further developed or supported. Use helpwin.
htpp
Use helpwin.
inquire
Use set and get to obtain the current state of an object or of MATLAB.
inverf
Rename to erfinv.
isdir
Use dir.

layout
No replacement in Release 11 (MATLAB 5.3).
loadhtml
Use helpwin or doc.
matq2ws
Replaced by assignin and evalin.

matqdlg
Replaced by assignin and evalin.
matqparse
Replaced by assignin and evalin.
matqueue
Replaced by assignin and evalin.
menulabel
Bug in Handle Graphics is now fixed.
mexdebug
Rename to dbmex.
ode23p
Use ode23 with no left-hand arguments or set an output function with odeset.
polyline, polymark
Use the line object or plot.
printmenu
No replacement in Release 11 (MATLAB 5.3).
saxis
Use soundsc.
ws2matq
Replaced by assignin and evalin.

Table 2-2: Graphics Function Changes  
Function
Change
Action
figure
In MATLAB 4 if a figure extended past the top of the window, it was adjusted to be visible. MATLAB 5.0 performs no adjustment.
Avoid hardcoded Figure positions.
get
get(h,'currentfigure') and get(h,'currentaxes') no longer create a Figure or an Axes if one doesn't exist. They return [] in that case.
gcf and gca always return a valid handle. Use gcf and gca instead of the get function in this context.

In MATLAB 4 you could determine if a graphics object had a default value set by passing its handle in a query like get(gca,'DefaultAxesColor').
In MATLAB 5.0 make the query on the object's ancestor, e.g.,
get(gcf,'DefaultAxesColor') or get(0,'DefaultAxesColor')
plot
MATLAB 4 plots may have elements that are the wrong color. MATLAB 5.0 defaults to a white background on all platforms. (MATLAB 4 defaulted to black)
Use colordef to control your color defaults. Typically, you will put a call to colordef in startup.m. To get the MATLAB 4 defaults, use colordef none.

plot line styles c1 through c15 and i are no longer supported
Use a 1-by-3 RGB ColorSpec instead. i is the same as get(gca,'color') or get(gcf,'color') when the Axes color is 'none'.
rotate
rotate alpha is reversed from MATLAB 4.
If your call looked like
rotate(h,[theta phi],alpha), change to
rotate(h,[theta phi],-alpha[0 0 0]).
text
text(S) when S is a multirow character array formerly produced one handle per row. Now it produces one multiline text handle.
Rewrite code so that it doesn't assume a specific number of handles.
uicontrol
The default Uicontrol text horizontal alignment is centered in MATLAB 5.0. (In MATLAB 4 we used to left align text and ignore the alignment property.)
Explicitly set the horizontal alignment when you create Uicontrol Text objects.

In MATLAB 4, Uicontrols of style 'edit' executed their callback routine whenever you moved the pointer out of the edit box. In MATLAB 5.0, edit controls execute their callbacks after you perform a specific action.
The callback is called when:
Return key is pressed (single-line edits only)

Focus is moved out of the edit by:

Clicking elsewhere in the Figure (on another Uicontrol or on another graphical object)

Clicking in another Figure

Clicking on the menu bar (X Window systems only)

Note: The following change does not directly apply to a specific function.

MATLAB 5.0 sets font size selection to match platform conventions. A MATLAB 4 font selection may be a different size in MATLAB 5.0.
Resize font appropriately.
Table 2-3: Graphics Property Changes  
Property
Object Type
Change
Action
AspectRatio
Axes
Obsolete
Replace with
DataAspectRatio and
PlotBoxAspectRatio.
BackgroundColor
Uimenu
Obsolete
Do not use.
CurrentMenu
Figure
Becoming obsolete. No warning message produced.
Replace with the function gcbo.
EraseMode
Image, Line, Patch,
Surface, Text

Now use xor against the Axes color rather than the Figure color.
For non-normal
erasemode, MATLAB recomputes Axes limits only when you fully update the display (e.g., with a drawnow command).
Modify code as appropriate.

Set the Axes limits (and other properties you depend upon) before using non-normal modes to create animation.
ExpFontAngle
Axes, Text
Obsolete
Do not use.
ExpFontName
Axes, Text
Obsolete
Do not use.
ExpFontSize
Axes, Text
Obsolete
Do not use.
ExpFontStrikeThrough
Axes, Text
Obsolete
Do not use.
ExpFontUnderline
Axes, Text
Obsolete
Do not use.
ExpFontUnits
Axes, Text
Obsolete
Do not use.
ExpFontWeight
Axes, Text
Obsolete
Do not use.
FontStrikeThrough
Axes, Text
Obsolete
Do not use.
FontUnderline
Axes, Text
Obsolete
Do not use.
LineStyle
Line, Patch, Surface
Setting the LineStyle property to a marker value (such as '+') now produces a warning.
Setting the marker style of a line now affects the
Marker property instead of the LineStyle property. Although you will be able to set a line marker using the LineStyle property (with a warning), you will not be able to get marker style information from
LineStyle.
Set the Marker property instead. Note that plot will continue to take line-color marker line styles.
If your code relies on markers in the
LineStyle, you'll have to change it to use the
Marker instead.

NextPlot
Axes, Figure
Use of value 'new' is obsolete. Produces warning message.
Use HandleVisibility to protect user interfaces from command line users.
PaletteMode
Image, Patch, Surface
Renamed
Use CDataMapping
RenderLimits
Axes
Obsolete
Do not use. Limits are now always accurate.
SelectionType
Figure
Right mouse button went from Extended in MATLAB 4 to Alternate in MATLAB 5.0.
None required.
Units
Axes, Figure, Text, Uicontrol
Units/Position is always order dependent for all objects. In MATLAB 4, it was inconsistent.
The Units property should precede any properties that depend upon it. A command such as axes('position',[100200300 100],'units',
'pixels')
is not the same as axes('units','pixels,'position',[100 200 300 100]). In the first case the numbers are interpreted in normalized coordinates.
WindowID
Figure
Possibly becoming obsolete.
May be removed in a future release.
XLim, XTick
Axes
Values must be monotonically increasing.
Sort the ticks:
set(gca,'xtick',
sort ([3 2 1])

XTickLabels
Axes
Renamed
Use XTickLabel

YLim, YTick
Axes
Values must be monotonically increasing.
Sort the ticks:
set(gca,'ytick',
sort ([3 2 1])

YTickLabels
Axes
Renamed
Use YTickLabel

ZLim, ZTick
Axes
Values must be monotonically increasing.
Sort the ticks:
set(gca,'ztick',
sort ([3 2 1])

ZTickLabels
Axes
Renamed
ZTickLabel



[ Previous | Help Desk | Next ]