Hobbies And Interests
Home  >> Science & Nature >> Science

How Do You Create a Function in Matlab?

Matlab is a software package with scripting language used in scientific data analysis. It is widely used in physics, engineering, biology, chemistry, finance and economics. Matlab originally stood for "Matrix laboratory" and was mainly used for performing matrix operations. Matlab allows the user to declare his own function and use it in code. Each new function will be different, depending on what the user wants it for. As an example, create a function called "meanaverage" that takes a set of numbers and finds their mean average.

Things You'll Need

  • Matlab software
Show More

Instructions

    • 1

      Click the Matlab icon on your desktop and wait until Matlab has started up.

    • 2

      Click the "File" button in the top-left hand corner of the Matlab development environment. Click "New" in the drop-down menu. The Matlab "Editor" window will open. Click inside the "Editor" window so the cursor is visible.

    • 3

      Type the word "function" in the Editor window. Type the name of the output within square brackets. In the case of this example, this is "[ average ]". Type an equal sign and the name of your function. In this case, it is: " = meanaverage(x);".

    • 4

      Press the "Enter" key on your keyboard. On the next line, type the definition of the function. In this case, it is: "n = length(x); s = sum(x); average = s/n;".

    • 5

      Click the "File" tab in the top-left corner of the Editor window. Click "Save As" on the resulting drop- down menu. Type "meanaverage" in the "Save As" dialogue box. This will create a Matlab file called "meanaverage.m" that contains a function used in Matlab code or in other Matlab files.


https://www.htfbw.com © Hobbies And Interests