MATLAB 5.2 Product Family New Features | Search  Help Desk |
Roadmap for Different Migration Routes
The sections of this chapter that you need to read depend on the version of MATLAB from which you are upgrading.If You Are Upgrading to MATLAB 5.2 from... |
Read These Sections ... |
MATLAB 5.1 |
"Upgrading from MATLAB 5.1 to MATLAB 5.2" |
MATLAB 5.0 |
"Upgrading from MATLAB 5.0 to MATLAB 5.2" "Upgrading from MATLAB 5.1 to MATLAB 5.2" |
MATLAB 4 |
All |
Upgrading from MATLAB 5.1 to MATLAB 5.2
This section describes some changes you can make to your code to eliminate error messages and warnings due to incompatible and noncompliant statements. Theclear
function does not remove an M-file from the MATLAB workspace if that M-file is locked with the mlock
function, introduced in MATLAB 5.2.
Although pre-Version 5.2 code does not use mlock
, if that code is modified to use mlock
, clear
will not behave as it did in previous versions of MATLAB (i.e., it will not be guaranteed to clear all M-files in the workspace). To unlock an M-file, use munlock
.
try, catch, and persistent Are Now Keywords
You can no longer usetry
, catch
, and persistent
as variable names in MATLAB 5.2. In previous releases MATLAB did not treat these as keywords.
In pre-Version 5.2, for
A(:) = bwhere
b
is a scalar or vector, the resulting type was the type of b
. In MATLAB 5.2, the resulting type is the type of A
. So, if A
is a uint8
array to begin with, and b
is a double
, the result is that A
is still a uint8
.
This change to preserve A
's type was made to ensure consistent indexing behavior.
When an object inherits from two different classes, MATLAB 5.2 performs a depth-first search when a method is called (this is how the method search was documented as working in previous versions). MATLAB 5.2 exhausts the search in one parent, then goes up the class hierarchy for each class from which the object inherits (from left to right, as appears in the class definition). In previous releases, MATLAB actually performed a modified breadth-first search.
In this hierarchy:
If function foo
is called:
foo
would be invoked.
foo
would be invoked.
API Memory Management Compatibility Issue
To address performance issues, the internal MATLAB memory management model has been changed somewhat. These changes support future enhancements to the MEX-file API. With this release, MATLAB now implicitly callsmxDestroyArray
, the mxArray
destructor, at the end of a MEX-file's execution on any mxArray
s that are not returned in the left-hand side list (plhs[]
). You are now warned if MATLAB detects any misconstructed or improperly destroyed mxArray
s.
We highly recommend that you fix code in your MEX-files that produces any of the warnings discussed in the following sections. For additional information, see the "Memory Management" section in Chapter 3 of the Application Program Interface Guide.
The rest of this discussion describes situations in which you would receive such warning messages. The discussion of each situation includes an example and a solution.
Improperly Destroying an mxArray
You cannot usemxFree
to destroy an mxArray
.
Warning: You are attempting to call mxFree on a <class-id> array. The destructor for mxArrays is mxDestroyArray; please call this instead. MATLAB will attempt to fix the problem and continue, but this will result in memory faults in future releases.Note: In MATLAB 5.2, these warnings are enabled by default for compatibility reasons. They can be disabled with the command
feature('MEXFileCompat',0)
mxArray *temp = mxCreateDoubleMatrix(1,1,mxREAL); ... mxFree(temp); /* INCORRECT */
mxFree
does not destroy the array object. This operation frees the structure header associated with the array, but MATLAB will still operate as if the array object needs to be destroyed. Thus MATLAB will try to destroy the array object, and in the process, attempt to free its structure header again.
mxDestroyArray
instead:
mxDestroyArray(temp); /* CORRECT */
Incorrectly Constructing a Cell or Structure mxArray
You cannot callmxSetCell
or mxSetField
variants with prhs[]
as the member array.
Warning: You are attempting to use an array from another scope (most likely an input argument) as a member of a cell array or structure. You need to make a copy of the array first. MATLAB will attempt to fix the problem and continue, but this will result in memory faults in future releases.
>> myfunction(When the MEX-file returns, MATLAB will destroy the entire cell array. Since this includes the members of the cell, this will implicitly destroy the MEX-file's input arguments. This can cause several strange results, generally having to do with the corruption of the caller's workspace, if the right-hand side argument used is a temporary array (i.e., a literal or the result of an expression).'
hello'
) /* myfunction is the name of your MEX-file and your code */ /* contains the following: */ mxArray *temp = mxCreateCellMatrix(1,1); ... mxSetCell(temp, 0, prhs[0]); /* INCORRECT */
mxDuplicateArray
and use that copy as the argument to mxSetCell
(or mxSetField
variants); for example:
mxSetCell(temp, 0, mxDuplicateArray(prhs[0])); /* CORRECT */
Creating a Temporary mxArray with Improper Data
You cannot callmxDestroyArray
on an mxArray
whose data was not allocated by an API routine.
Warning: You have attempted to point the data of an array to a block of memory not allocated through the MATLAB API. MATLAB will attempt to fix the problem and continue, but this will result in memory faults in future releases.
mxSetPr
, mxSetPi
, mxSetData
, or mxSetImagData
with memory as the intended data block (second argument) that was not allocated by mxCalloc
, mxMalloc
, or mxRealloc
, as shown below:
mxArray *temp = mxCreateDoubleMatrix(0,0,mxREAL); double data[5] = {1,2,3,4,5}; ... mxSetM(temp,1); mxSetN(temp,5); mxSetPr(temp, data); /* INCORRECT */then when the MEX-file returns, MATLAB will attempt to free the pointer to real data and the pointer to imaginary data (if any). Thus MATLAB will attempt to free memory, in this example, from the program stack. This will cause the above warning when MATLAB attempts to reconcile its consistency checking information.
mxSetPr
to set the data pointer, instead create the mxArray
with the right size and use memcpy
to copy the stack data into the buffer returned by mxGetPr:
mxArray *temp = mxCreateDoubleMatrix(1,5,mxREAL); double data[5] = {1,2,3,4,5}; ... memcpy(mxGetPr(temp), data, 5*sizeof(double)); /* CORRECT */Prior to Version 5.2, if you created an
mxArray
using one of the API creation routines and then you overwrote the pointer to the data using mxSetPr
, MATLAB would still free the original memory. This is no longer the case.
For example:
pr = mxCalloc(5*5, sizeof(double)); ... <load data into pr> plhs[0] = mxCreateDoubleMatrix(5,5,mxREAL); mxSetPr(plhs[0], pr); /* INCORRECT */will now leak 5*5*8 bytes of memory, where 8 bytes is the size of a
double
.
You can avoid that memory leak by changing the code:
plhs[0] = mxCreateDoubleMatrix(5,5,mxREAL); pr = mxGetPr(plhs[0]); ... <load data into pr>or alternatively:
pr = mxCalloc(5*5, sizeof(double)); ... <load data into pr> plhs[0] = mxCreateDoubleMatrix(5,5,mxREAL); mxFree(mxGetPr(plhs[0])); mxSetPr(plhs[0], pr);Note that the first solution is more efficient. Similar memory leaks can also occur when using
mxSetPi
, mxSetData
, mxSetImagData
, mxSetIr
, or mxSetJc
. You can address this issue as shown above to avoid such memory leaks.
Recommendation: MEX-Files Should Destroy Their Own Temporary Arrays
In general, we recommend that MEX-files destroy their own temporary arrays and clean up their own temporary memory. All inconsistentmxArray
s except those returned in the left-hand side list and the return from the mxGetArrayPtr
may be safely destructed. This approach is consistent with other MATLAB API applications (i.e., MAT-file applications, Engine applications, and MATLAB Compiler generated applications).
Upgrading from MATLAB 5.0 to MATLAB 5.2
This table describes some changes you can make to your code to eliminate error messages and warnings due to incompatible and noncompliant statements in MATLAB 5.0 code that you are upgrading to MATLAB 5.2. Note: If you are upgrading from MATLAB 5.0 to MATLAB 5.2, in addition to this section, you should read the previous section, called "Upgrading from MATLAB 5.1 to MATLAB 5.2."Upgrading from MATLAB 4 to MATLAB 5.2
MATLAB 5.0 was a major upgrade to MATLAB 4. Although The MathWorks endeavors to maintain full upwards compatibility between subsequent releases of MATLAB, inevitably there are situations where this is not possible. In the case of MATLAB 5.0, there were a number of changes that you need to know about in order to migrate your code from MATLAB 4 to MATLAB 5.0. Note: In addition to this section, you should read the rest of this chapter as well if you are upgrading from MATLAB 4.0 to MATLAB 5.2.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.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. Unless you know the screen size, it is better to position the Figure by first querying the Root ScreenSize property. |
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 white background on the Macintosh and black everywhere else.) |
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. (MATLAB 4 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 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. |
Converting MATLAB 4 External Interface Programs to the MATLAB 5.0 Application Program Interface
MATLAB 4 External Interface programs, including MEX-files, MAT-file programs, and Engine programs may run without any modification on the MATLAB 5.0 Application Program Interface (API), or they may require modification and/or recompilation. The following pages and flowcharts describe how to determine which of these possibilities applies in your situation and how to choose the appropriate conversion technique.mxChar
, a 16-bit unsigned integer data type. If the mxArray
's class is mxCHAR_CLASS
, the API treats each number in the mxArray
as an element from the current character set. Character sets are platform specific.
External Interface programs that call the API routines mxGetString()
and mxCreateString()
to manipulate strings continue to work. All MEX-files that directly manipulate strings must be rewritten. MAT-file and Engine applications that directly manipulate strings need not be rewritten. However, to recompile these applications under MATLAB 5.0, any code that directly manipulates strings must be rewritten. We highly recommend that you use the API string access and creation routines to do this. To help in this endeavor, you can use the new C routine, mxCreateCharMatrixFromStrings()
, in addition to mxGetString()
and mxCreateString()
, to make it easy to create two-dimensional string matrices.
myeig(A,B,C)
, that calculates eigenvalues of three matrices. In MATLAB 4, if matrix A
is complex, B
and C
are assumed to be complex matrices as well. In this instance, additional memory is allocated for the complex part of B
and C
, and initialized with zero values.
MATLAB 5.0 does not allocate this memory for you. If your MEX-file assumes argument complexification, you will have to rewrite your MEX-file. Each argument to a MEX-function needs to be tested with mxIsComplex()
to guarantee that an argument indeed has a complex component.
mxIs*
routines and mxGetNumberOfDimensions()
have been added to the C interface so that now you can explicitly check arguments for specific data types and shapes. mxIsDouble()
, which always returned 1
in MATLAB 4, now correctly returns 1
only if the mxArray
is of type double precision floating point. mxIsDouble()
is available in C as well as Fortran. mxGetN()
returns the total number of elements in dimensions 2-n, and therefore works correctly with multidimensional as well as two-dimensional arguments.
-v3.5
switch to the cmex
or fmex
script are no longer supported and must be rewritten. Refer to both the MATLAB 4 External Interface Guide for information on upgrading to the MATLAB 4 syntax and the section later in this document on recoding for MATLAB 5.0 compliance.
mex
users on Windows, 68K Macintosh and the Sun4 must recompile their applications using the mex
script for programs that call mxCreateString()
or mxGetString()
. Only these platforms, which statically link in the Fortran interface, are affected. No recompilation is required on other platforms.
cmex
or fmex
from within MATLAB by using the !
command or execute in another window. These methods are no longer supported with MATLAB 5.0. However, the M-file that builds MEX-files, mex.m
, is available on all platforms and can safely rebuild loaded MEX-files by unloading them before proceeding with the build. The interface provided by the M-file is identical to the external mex
script.
-debug
switch to cmex
/fmex
is no longer supported. Instead, on all platforms, MATLAB 5.0 supports MEX-file debugging from within a MATLAB session. (See the debugging sections of the MATLAB Application Program Interface Guide for more details). In addition, the mex
script has been enhanced with the -argcheck
switch. That switch provides a way for C mex
users to generate code to check input and output arguments at runtime and issue appropriate error messages if invalid data is detected.
-v4
switch to the save
command.
Microsoft Windows Considerations
MEX-files are generated under MATLAB 5.0 as 32-bit DLLs. Thecmex
and fmex
batch files have been superseded by mex
(a PERL script) and a set of compiler-specific option files. These compilers are fully supported for creating MEX or stand-alone applications through MathWorks-supplied option files:
cmex
and fmex
Bourne shell scripts for building MEX-files have been superseded by mex
, also a Bourne shell script, that sources an options file, mexopts.sh
, for all platform compiler-specific information. The options file contains all the pertinent compiler and linker switches for building ANSI C and Fortran MEX-file applications. The .mexrc.sh
file is no longer supported and must be converted to the new format. The mexdebug
MATLAB command that allows UNIX users to debug their MEX-files while MATLAB is running has been changed to dbmex
. The behavior of dbmex
under MATLAB 5.0 is identical to mexdebug
under MATLAB 4.
See "Fortran MEX-file Considerations" above for Sun4-specific restrictions on creating and accessing MATLAB character strings.
You can use your existing UNIX Engine and MAT-file binary files unmodified; no recompilation is necessary. Note that MATLAB 4 Engine programs have no access to new MATLAB 5.0 data types. If you try to invoke engGetMatrix(ep,
my_variable
)
, and my_variable
is a cell array, structure array, object, etc., the operation automatically fails.
The Absoft MacFortran and Symantec Think C compilers are no longer supported, as of MATLAB 5.0.
MEX-files built under MATLAB 4 with MPW C on the 68K Macintosh that call the following I/O routines must be recompiled: putc()
, getc()
, ferror()
, feof()
, clearerr()
. These exist as macros in MPW's stdio.
h. In MATLAB 5.0, these functions have been reimplemented as function callbacks to MATLAB.
See "Fortran MEX-file Considerations" above for 68K Macintosh-specific restrictions on creating and accessing MATLAB character strings.
MEX-files compiled for MATLAB 4 on the VMS platform are not binary compatible. You must regenerate these files from source code before using them with MATLAB 5.0.
-V4
option of mex
. This option uses mex
to define a macro V4_COMPAT
that supports MATLAB 4 syntax and function calls. Therefore, any ANSI C MEX-file source code that compiled cleanly under MATLAB 4 should compile cleanly with the -V4
option. The resulting MEX-file should run under MATLAB 5.0 just as it ran under MATLAB 4. For example, given C MEX-file MATLAB 4 source code in file MyEig.c
, recompiling under UNIX with
mex -V4 myeig.cyields a MEX-file that MATLAB 5.0 can execute. It is also possible to use
cmex
and fmex
for compiling C and Fortran source code, but both of these functions simply call mex
.
The obvious advantage to the -V4
strategy is that it requires very little work on your part. However, this strategy provides only a temporary solution to the conversion problem; there is no guarantee that future releases of MATLAB will continue to support the -V4
option. If you have the time, recoding for -DV4_COMPAT
. This allows you to avoid recoding, such as rewriting obsolete function calls as their MATLAB 5.0 equivalents. However, the resulting program will have the same restrictions as existing binary files.
See the MATLAB Application Program Interface Guide for instructions on compiling stand-alone programs in MATLAB 5.0.
Recoding C Code for MATLAB 5.0 Compliance
Recoding your MATLAB 4 C code for MATLAB 5.0 compliance involves:Matrix
variables to mxArray
variables.
The MATLAB 4 Matrix
data type is obsolete; you must change all Matrix
variables to mxArray
variables. For example, the mxCreateSparse
function returns a Matrix pointer in MATLAB 4:
Matrix *MySparse; MySparse = mxCreateSparse(10,10,110,REAL);
To be MATLAB 5.0 compliant, change the code to:
mxArray *MySparse; MySparse = mxCreateSparse(10,10,110,mxREAL);
The function prototype of almost every MATLAB 4 function is different in MATLAB 5.0. The two primary prototype changes are
Matrix
arguments are now mxArray
arguments.
const *
.
mexFunction
to take a constant mxArray *
as a fourth argument.
REAL
to mxREAL
and COMPLEX
to mxCOMPLEX
.
In any function that requires the specification of real or complex data types, instead of REAL
and COMPLEX
, use mxREAL
and mxCOMPLEX
. For example, in MATLAB 4 you would write
mxCreateSparse(m,n,nzmax,REAL);
to create an m-by-n sparse matrix with nzmax
nonzero real elements. In MATLAB 5.0, the correct syntax for this same function is:
mxCreateSparse(m,n,nzmax,mxREAL);
A number of functions have become obsolete. However, MATLAB 5.0 offers replacements for nearly all obsolete functions.
You should explicitly check the data type of your input arguments to ensure that you have what you want.
Upgrading Toolboxes and Blocksets
The versions of the toolboxes and blocksets shipped with MATLAB 5.2 are compatible with the previous versions of the respective toolboxes/blocksets. However, there are a minor upgrade issues for theFuzzy Logic Toolbox: Updating FIS Models
In the Fuzzy Logic Toolbox 2.0, the Fuzzy Inference System (FIS) is represented as a MATLAB structure. A structure (instead of a flat matrix) is now the basic element in constructing a fuzzy logic system. This fundamental change in the way of representing fuzzy logic system makes many details of working with the constructed system easier. A Fuzzy Inference System that you created with a pre-2.0 version of the Fuzzy Logic Toolbox is still usable in 2.0, if you run theconvertfis
function on it. The convertfis
function automatically converts pre-2.0 Fuzzy Inference Systems to work with Version 2.0.
DSP Blockset: Upgrading from Version 1.0a
The DSP Blockset 2.2 is completely compatible with Version 1.0a of the blockset, and Version 2.2 blocks can be used directly in models containing Version 1.0a blocks. However, because of changes to the buffering operation in 2.2, you should avoid using 2.2 buffer blocks in the same model with 1.0a buffer blocks. If you wish to continue using the 1.0a Buffer, Complex Buffer, Unbuffer, and Complex Unbuffer blocks in a model, do not use the 2.2 version of these blocks, or the 2.2 Partial Unbuffer and Complex Partial Unbuffer blocks. If you want to use the 2.2 versions in your model, we recommend that you replace any 1.0a buffering blocks with the 2.2 versions as well. Also note that if you have custom blocks that use C-MEX S-functions, you will need to recompile the C-MEX S-functions to work with Simulink 2.2. All of the Version 2.2 DSP Blockset C-MEX S-Functions are completely compatible with Simulink 2.2. Additionally, DSP Blockset 2.2 includes updated C-MEX S-functions for the Version 1.0a blocks that use them, so that these 1.0a blocks will continue to work with Simulink 2.2. However, the updated 1.0a C-MEX S-functions will generate warnings indicating that these functions are obsolete, and we recommend that you replace these old blocks with the 2.2 versions when convenient.