|
Do
you find it difficult to write MATLAB codes that runs across
multiple MATLAB releases?
In MATLAB Version 7.4, there is a new verLessThan
function that compares the specified version number and toolbox
name with the version of that same toolbox that is currently
running.
verLessThan(toolbox,
version) returns logical 1 (true) if the version of the toolbox
specified by the string toolbox is older than the version
specified by the string version, and logical 0 (false) otherwise.
The below
examples illustrate the proper usage of the verLessThan
function.
Example
1 - Checking For the Minimum Required Version
if verLessThan('simulink',
'4.0')
error('Simulink
4.0 or higher is required.');
end
Example
2 - Choosing Which Code to Run
if verLessThan('matlab',
'7.0.1')
% --
Put code to run under MATLAB 7.0.0 and earlier here --
else
% --
Put code to run under MATLAB 7.0.1 and later here --
end
Remarks
The verLessThan function is available with MATLAB Version
7.4. If you are running a version of MATLAB earlier than 7.4,
you can download the verLessThan M-file from the following
MathWorks Technical Support solution. You must be running
MATLAB Version 6.0 or higher to use this M-file:
http://www.mathworks.com/support/solutions/data/1-38LI61.html?solution=1-38LI61
Debugging
MATLAB M-files from the MATLAB Command Prompt
What
Debugging Tools Are Available at the MATLAB Command Prompt?
This section describes how to make use of functions to
debug programs from the MATLAB command prompt. There are altogether
seven topics and in the e-newsletter issue, we will look at
the sixth and seventh topic.
| Topics |
| 1. |
Setting,
Clearing, and Querying Breakpoints |
| 2. |
Moving
from Workspace to Workspace |
| 3. |
Executing
Your Code Using the DBSTEP Function |
| 4. |
Displaying
Status Messages Periodically |
| 5. |
Using
the TRY/CATCH Block to Capture Errors |
| 6. |
Using
the ERROR Function with the LASTERR and RETHROW Functions |
| 7. |
The
WHICH Function |
Topic 6: Using the ERROR Function with the LASTERR and RETHROW
Functions
Inside your TRY/CATCH block, you may want to proceed differently
based on which specific error message you received; for instance,
trying to open a nonexistent file would lead to a
File
not found
error.
You may want to allow the user to select a new filename to
open rather than proceeding or terminating the program completely.
You can determine the specific error that triggered the CATCH
block using the LASTERR function.
Alternatively,
after performing your error cleanup inside the CATCH block,
you may want to trigger the error that forced you into the
CATCH block, to alert the user that something has gone wrong.
You can do this using the ERROR function (which explicitly
throws an error) in combination with the LASTERR function
or with the functions RETHROW and LASTERROR.
You can see a sample function file that demonstrates the
use of the LASTERR, LASTERROR, and RETHROW
functions as follows:

Topic
7: The WHICH Function
The WHICH
function will display the location of the first file on the
MATLAB path whose name matches the string that was passed
to WHICH. This function will also tell you if the string is
the name of a variable in the current workspace or is the
name of a built-in function. You can also provide the WHICH
function an additional argument, the -ALL flag, which will
list all the variables, built-in functions, and files whose
names match the string provided to WHICH. This allows you
to verify that you are using the correct version of a file
when there may be multiple files with the same name on the
MATLAB path.
One of
the most common cases where the WHICH function is useful in
finding a problem with your code occurs when attempting to
call a function. If another function with that name is located
higher on the MATLAB search path or a variable with the function
name exists, MATLAB will use the other function or the value
of the variable instead of the function you intend.
For example,
if you define a variable why
>>
why = 1:10;
and attempt
to call the MATLAB built-in WHY function
>>
why(37)
you will
receive the following error:
???
Index exceeds matrix dimensions.
It is
easy to use WHICH to find out whether why is a function or
a variable. If you now type
>>
which why
you now
see
why
is a variable.
rather
than what is expected
C:\MATLAB\R2007a\toolbox\matlab\elmat\why.m
|