Active 10 years, 8 months ago. Viewed 51k times. Improve this question. Add a comment. Active Oldest Votes. Example One of the main differences between a script and a function is access to variables in the workspace. Improve this answer. Community Bot 1 1 1 silver badge.
Jacob Jacob Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. You can execute these just like any other function in matlab. These functions exist in files whose name ends in a ".
Most of the functions that are used in matlab are actually just m-files. Therefore, you can see how they work and modify them to suit your needs. To list a m-file, just. When you write your own files, it is useful to use this "help" facility to remind you of its syntax and purpose. Here is an example of a simple m-file that will scramble the elements in a vector, and output both the scrambled vector and a vector containing indices used to descramble the vector.
If you leave out the "function" line, then a mfile is considered to be a "script". All variables defined in the parent workspace are accessible to the script and can be modified by the script. In this sense, all variables are considered global in a script. Moreover, every time a calculation needs to be repeated, all commands have to be retyped and re-entered.
This is inconvenient, and the reader who has perused the preceding chapters has undoubtedly experienced it. The remedy lies in writing all commands sequentially into a file, saving it and running it as necessary. These types are explained further below.
A script file is a sequence of commands often called a program that should be run. Corrections can be entered directly in the file and new commands can be added.
After this the Editor Window appears Figure 4. Figure 4. The commands of the script file should be typed line by line. It is also possible to type two or more commands on the same line, dividing them by commas or by semicolons.
A new line will be accessible after pressing the Enter key. Each new line is numbered automatically. A command can also be typed in another editor and then copied into the Editor Window.
The commands appear in black for better legibility. The bar is topped by the message indicator. A green indicator means no errors, warnings or possibility of improvements; a red indicator means syntax errors have been detected; and an orange indicator means warnings or possibility of improvements but no errors.
When the cursor is placed at this line the comment message appears see Figure 4. The script file should be saved after it has been written into the Editor Window. In addition, it is not recommended to introduce spaces into the name, which cannot be longer than 63 characters. To execute a script file: first, the user should check the current MATLAB directory, and if the file is not in it a directory with this file should be set; secondly, the file name should be typed in the Command Window and then entered.
The simplest way to achieve the desired setting is:. For example, the script file with name EditorExample1. The result of calculations via the commands is saved in the script file, and is displayed in the Command Window. To assign a value to the variables defined in the script file when the script is run the input command should be used; this command has the forms:. The file is saved with the pound2kg name.
The run command, prompts, and input and output numbers are:. With the input command the vectors and matrices can be inputted in the same way as for a variable — numbers in square brackets. When these parameters are assigned, the value of y is obtained.
Not only a specific expression, but a complete program created by the user can be defined as a function and saved as a function file. The word function appears in blue and must be the first word in the file. The input parameters must be written in parentheses and the output parameters in square brackets.
In the case of a single output parameter the brackets can be dispensed with. Parameters must be divided by commas. As with a script file, a function file should be written in the Editor Window. An example of a function file is presented in Figure 4. This function is named dilution and calculates the molar concentration of a solute after dilution. It has three input parameters — the molar concentration M 1 and the solution volume V 1 before dilution, and solution volume V 2 after dilution — and one output parameter — the molar concentration of the solute after dilution.
The requirements and recommendations regarding the structure and certain parts of the function file are:. In addition to the previous comments regarding the function definition, the function can be written with the parameters omitted completely or in part. Possible variants of the function definition line are given in the following examples:. Number and names of the input and output parameters can differ from those in the examples.
These lines are placed just after the function definition line and before the first non-comment line. The first of these lines should contain a short definition of the function used by the lookfor command when it searches the information. The help comments are displayed when the help command is introduced with the user-defined function name; for example, typing into the Command Window help dilution yields:. The function body can contain one or more commands for actual calculations; between these commands, usually at their end, should be placed the assignments to the output parameters; for example, in the example in Figure 4.
The actual values must be assigned to the input parameters before running the function. The variables in the function file are local and relevant only within the file. Each time you run it, the script stores the result in a variable named a that is in the base workspace.
However, instead of manually updating the script each time, you can make your code more flexible by converting it to a function. Replace the statements that assign values to b and h with a function declaration statement.
The declaration includes the function keyword, the names of input and output arguments, and the name of the function. After you save the file, you can call the function with different base and height values from the command line without modifying the script:. Functions have their own workspace, separate from the base workspace.
Therefore, none of the calls to the function triarea overwrite the value of a in the base workspace.
0コメント