3. Script M-files

Using M-files

To create a script M-file, click on “File” in the MATLAB Command Window, then on “New” followed by “M-file”. This opens the M-file “Editor/Debugger” window, where you can enter your sequence of MATLAB commands, in simple text format without any prompts. As a first example, reconsider the problem of finding the intercepts on the axes this section. Type in the following:

clear all

x1=3

y1=4

x2=8

y2=7

%let m be the slope of the line y=m*x+b

m=(y2-y1)/(x2-x1);

% y-y1=m*(x-x1) intersects y-axis when x=0

% this gives b=y1-m*x1

y_intercept=y1-m*x1

x_intercept=x1-y1/m

Now save this file as described earlier. Save to your flash drive. You will note that the header now shows the directory location as well as example1.m as the name of this M-file.

Return to the MATLAB Command Window and enter example1 after the prompt. Assuming you have changed to the same directory, this will execute the sequence of commands in example1.m.

Return to the “Editor/Debugger” window. Use the mouse, arrows, backspace and delete facilities to change x1,y1,x2,y2 to the values specified in the second exercise in this Section. Then Save and again enter example1 after the >> prompt. Of course this can most simply be accomplished using the up-arrow once.