Skip to content Skip to sidebar Skip to footer

Run a Script From Start Again Matlab

Introduction

In previous lessons, you learned how to interact with MATLAB in the MATLAB command window by typing commands at the control prompt. And, you saw how you could ascertain and utilize variables. Notwithstanding, allow'southward say you have a sequence of commands which you find that you lot utilise over and over once more, in many dissimilar MATLAB sessions. It would exist squeamish if you didn't have to manually type these commands at the command prompt whenever you lot want to use them. This is 1 of the things that scripts can practise for you. A MATLAB script is an ASCII text file (if you lot don't know what an ASCII text file is, read on) that contains a sequence of MATLAB commands; the commands contained in a script file can be run, in guild, in the MATLAB command window but by typing the name of the file at the command prompt. In this lesson, we will merely exist introducing scripts with simple examples; even so, MATLAB is essentially a general-purpose programming language (like BASIC, C, Java, Pascal, or FORTRAN), and in their total generality scripts are programs written in the MATLAB language. And then in learning to write scripts, yous are basically learning computer programming!

Yous can use whatsoever text editor, such as Microsoft Windows Notepad, or give-and-take processor, such as Microsoft Word, to create scripts, but you lot must make sure that you save scripts as elementary text documents (i.east., in the "Save As" dialogue box, cull "Text Certificate" or its equivalent for "Salvage as type:"). If you don't know what ASCII means, don't worry about it; basically it just means a simple text document (i.due east., when you choose "Text Document" for "Save as blazon:" in the "Save Every bit" dialogue box, yous are saving as an ASCII text file). Information technology will probably just be easiest to create your scripts using MATLAB'due south built-in text editor, which will be described subsequently in this lesson and which automatically merely saves files as ASCII text files for you. Also, when you name your script files, y'all volition demand to suspend the suffix ".m" to the filename, for example "my_script.grand". Scripts in MATLAB are also called "m-files" because of this, and the ".m" suffix tells MATLAB that a file is a script (there is another use of m-files, to create your own functions, which yous will larn about in a later lesson).

A Simple Script

So permit's look at a simple example of a script that calculates the average of five numbers that are stored in variables. Here is the contents of a script file "average_script.m" that was created with MATLAB's built-in text editor:

%a uncomplicated MATLAB grand-file to calculate the average of 5 numbers.

%first define variables for the 5 numbers:
a = five;
b = 10;
c = xv;
d = 20;
e = 25;

%now calculate the average of these and print it out:

five_number_average = (a + b + c + d + eastward) / 5;
five_number_average

You should already empathise what the text in blackness does: it defines the five variables, calculates their boilerplate, and then prints out this boilerplate. The text in dark-green (i.due east., the lines starting with "%" --- all annotate lines must start with "%") are comments. Comments are completely ignored by MATLAB (i.e., getting rid of any or all of the 3 comment lines to a higher place would not alter what the script does at all), and are intended solely for you and others to read. They are a way for you to certificate important points most your scripts, so that, for example, if you put abroad some script for a long fourth dimension and then come up dorsum to it later on you can exist quickly and hands reminded of what information technology is you were trying to exercise, and how to use the script; comments are besides very useful in helping others to understand your scripts. You tin can likewise use comments in the MATLAB command window, merely they are more frequently used in scripts. Some good advice: brand liberal utilise of comments in your scripts. Notation that another good reason to utilise comments is that the comments yous place at the beginning of your scripts (chosen the header --- the header includes all comment lines up to the kickoff command) volition exist returned to users when they go help for your script; thus, you should e'er effort to place information about what your script is about and how to use it every bit comment lines at the showtime of your scripts. For example, if you place the following annotate lines right at the beginning of a script called "my_script":

%a MATLAB 1000-file which does ....
%Use this script to practise ....

then this text would be returned to users who blazon "help my_script" at the control prompt (assuming the directory where "my_script" is located is in matlabpath).

Running Scripts

If the above script "average_script.grand" is saved in the nowadays working directory, then it tin be run only by typing "average_script" at the command prompt in the MATLAB command window. Here is the event of running it:

>> clear
>> who
>> pwd

ans =

c:\my_scripts

>> dir

. .. average_script.grand


>> average_script

five_number_average =

fifteen

>> who

Your variables are:

a d
ans eastward
b five_number_average
c

>>

Notice that the variable definitions for a, b, c, d, e, and five_number_average do non print out results; this is because they were followed by semicolons. Also notice that defined variables are still divers and can exist used even after a script finishes running (the last "who" command above shows this).

MATLAB'due south Text Editor

Well, now that you know what scripts are and how to run them, let'south talk about how you create them with MATLAB's congenital-in text editor. MATLAB's text editor is similar to other text editors, simply it is peculiarly suited for creating MATLAB-specific files, such equally m-files. For case, it color codes dissimilar structural units of MATLAB scripts (e.g., as you saw in the above script which was created with MATLAB's built-in text editor, comments are greenish and command lines are black, and other constructs will utilize other colors) in lodge to organize the script for you, so, for example, you can find what you lot are looking for in the script more easily. In addition, it is important to note that MATLAB's text editor will automatically append the ".m" suffix to your script file for you, then yous don't need to append it yourself (but yous would have to append it yourself in other text editors, such equally Windows Notepad). The MATLAB text editor can also be used for debugging (i.eastward., finding and fixing errors in scripts), just we won't talk about that hither (the proper noun of the text editor is, in fact, "MATLAB Editor/Debugger").

You tin can get-go MATLAB's text editor in ii means. 1 way is to but blazon "edit" at the control prompt in the MATLAB command window. The other mode is to select "New" from the "File" menu in the MATLAB command window; this volition open up another carte, from which you should choose "M-file". These means of starting MATLAB's text editor will start it up with a new, empty document; if you desire to outset the editor upward with an existing file (e.g., a script file you have partially completed), then you can either type at the command prompt "edit" followed by the name of the file, or select "open" from the MATLAB control window's "file" carte du jour (and then choose the file yous want to open from the "open" dialoge box). The way you use MATLAB's text editor is substantially the same as how you lot would utilise Windows Notepad or other simple text editors (i.e., cutting and pasting, moving the cursor around the text, saving, etc. --- yous should already know how to do bones text editing in Windows). In any case, the MATLAB Editor/Debugger window looks like this:

A screenshot of a social media post    Description automatically generated

That is well-nigh all there is to it. Correct at present, you should practice using MATLAB's text editor. Go alee and starting time MATLAB's text editor, and blazon in the post-obit text for a very unproblematic script

:

%This is my practise script:

%Showtime define ii variables:
a = iii;
b = 4;

%then let'southward assume a and b are the lengths of two sides of a right %triangle and let's calculate the length of the other side (the %hypotenuse) using the Pythagorean formula:
c = sqrt(a*a + b*b);

%and, let's encounter the result:
c

After you type this text in, relieve information technology somewhere convenient (for example, if you have a thumb drive with you, save it in a directory on this solid state memory --- remember the complete path to wherever you saved it, though). Later on you salvage it, leave out of the text editor, and add the path to your script to matlabpath. (for example, if you saved your script on the thumb bulldoze and this happens to accept been assigned the drive alphabetic character �Due east:� and then this script may cease upwards being relieve as: "E:\my_scripts\practice_script.k" REMEMBER >>>> you would demand to execute the command "addpath Due east:\my_scripts" at the command prompt). Then blazon the name of your script without the ".k" suffix at the control prompt (for case, "practice_script") and you should become the output:

>> practice_script

c =

v

You might now want to experiment some more with this practice script (for example, add together more variables, try different functions such as sin, cos, etc.) To do that, just open your script up in MATLAB's text editor again, brand your changes, save the file, exit the text editor, and finally blazon the proper name of the script at the command prompt. Have fun!

To continue on to the next lesson, click here.

donaldsonmostright.blogspot.com

Source: http://et.engr.iupui.edu/~jschild/matlabtutorial/introductory_lessons/scripts_and_matlabs_text_editor.htm

Post a Comment for "Run a Script From Start Again Matlab"