M-Files
the Command Window and its history mechanism are insufficient in some cases
A much better approach is to create an M-file. There are two different kinds of M-files
1. script M-files
2. function M-files.
M-files are ordinary text files containing MATLAB commands where you can create and modify them.
you can use the built-in Editor/Debugger
Echoing Commands
echo:
the commands in a script M-file will not automatically be displayed in the Command Window. If you want the commands to be displayed along with the results, use echo
echo on
format long
x = [0.1, 0.01, 0.001];
y = sin(x)./x
echo off
Adding Comments.
to get the explanation of the command used
echo on
% Turn on 15 digit display
format long
x = [0.1, 0.01, 0.001];
y = sin(x)./x
% These values illustrate the fact that the limit of
% sin(x)/x as x approaches 0 is 1.
echo off
Structuring Script M-Files.
the script should be self-contained, unaffected by other variables that you might have defined elsewhere in the MATLAB session
clear all: you can type the line clear all at the beginning of the script, to ensure that previous definitions of variables do not affect the results.
close all command at the beginning of a script M-file that creates graphics, to close all graphics windows and start with a clean slate
% Remove old variable definitions
clear all
% Remove old graphics windows
close all
% Display the command lines in the command window
echo on
% Turn on 15 digit display
format long
Fine-Tuning Your M-Files
- Include clear all and close all at the beginning of the M-file.
- Use echo on early in your M-file so that you can see “cause” as well as “effect”.
- If you are producing graphics, use hold on and hold off carefully.
- In general, you should put a pause statement after each hold off.
- Otherwise, the next graphics command will obliterate the current one, and you won’t see it.
- Do not include bare print statements in your M-files. Instead, print to a file.
- Make liberal use of pause.
- Finally, remember that you can stop a running M-file by typing CTRL+C.This is useful if, at a pause or input statement, you realize that you want to stop execution completely.
No comments:
Post a Comment