Matlab With

LiveLink™ for MATLAB® allows you to utilize the full power of MATLAB and its toolboxes in preprocessing, model manipulation, and postprocessing: Enhance your in-house MATLAB code with powerful multiphysics simulations. Base your geometry modeling on probabilistic or image data. Perform arbitrary statistical analysis on simulation results. MATLAB - Functions - A function is a group of statements that together perform a task. In MATLAB, functions are defined in separate files. The name of the file and of the function s.

Content on these pages is derived from participant presentations, discussions, and breakout groups at theTeaching Computation with MATLAB workshops.
Computation is the use of computers to perform calculations, model data, make predictions, and test hypotheses. Computational skills are necessary to employ quantitative methods in complex scientific contexts and with large data sets. With today's unprecedented data availability there are numerous new science and engineering opportunities if one has the necessary computational skills. Developing these skills requires students to be comfortable with languages and tools such as MATLAB that enable them to express and explore solutions to scientific problems.

In order to provide students with the computational skills they need to be successful, the teaching of computation must change to reflect the changing landscape of computation. Teaching Computation in the Sciences Using MATLAB provides resources to help educators incorporate computation into their classrooms and engage with a community of educators interested in improving the teaching of these important skills.


Looking for opportunities to discuss teaching computation and programming with MATLAB? Participate in the annual Teaching Computation with MATLAB workshop. Join the email list to receive timely updates.

Approaches to Teaching Computation with MATLAB

MATLAB is useful tool for teaching computation because it allows for robust data analysis, data visualization and exploration, modeling, and working with real data sets. If the use of MATLAB is scaffolded in a course or curriculum, it is a tool that can help students develop computational skills and build self-efficacy. When combined with collaborative learning techniques and effective assessments, MATLAB can be an effective and powerful tool for teaching computation.

Computational Skills and Techniques


Strategies for Teaching Computation


Discover activities, essays, and course descriptions submitted by educators who teach with MATLAB. Use the search bar or explore the individual collections below.

Activities Collection

MATLAB based activities, laboratory exercises, projects, and other assignments.

Essays Collection

Educator perspectives on how teaching with MATLAB can enhance learning.

Courses Collection

Course descriptions and syllabi for STEM courses on (or that integrate) MATLAB.

Matlab With


Get Involved

In addition to hosting resources developed by educators for educators, Teaching Computation in the Sciences Using MATLAB is a growing community of faculty in STEM fields interesting in improving computation in teaching. Get involved by participating in events or joining the community discussion.

Workshops and Webinars

Participate in professional development events aimed at improving the teaching of computation. Workshops and webinars provide spaces for experienced educators to share their ideas and opportunities for those just getting started to gain expertise.

Join the Community

Join a community of educators interested in teaching computation with MATLAB. Whether you're just getting started or have been teaching with MATLAB for years, you can ask questions, share experience, and receive updates about upcoming events.

  • Matlab Tutorial
  • MATLAB Advanced
  • MATLAB Useful Resources
  • Selected Reading
Matlab

A function is a group of statements that together perform a task. In MATLAB, functions are defined in separate files. The name of the file and of the function should be the same.

Functions operate on variables within their own workspace, which is also called the local workspace, separate from the workspace you access at the MATLAB command prompt which is called the base workspace.

Functions can accept more than one input arguments and may return more than one output arguments.

Syntax of a function statement is −

Example

The following function named mymax should be written in a file named mymax.m. It takes five numbers as argument and returns the maximum of the numbers.

Create a function file, named mymax.m and type the following code in it −

The first line of a function starts with the keyword function. It gives the name of the function and order of arguments. In our example, the mymax function has five input arguments and one output argument.

The comment lines that come right after the function statement provide the help text. These lines are printed when you type −

MATLAB will execute the above statement and return the following result −

With

You can call the function as −

MATLAB will execute the above statement and return the following result −

Anonymous Functions

An anonymous function is like an inline function in traditional programming languages, defined within a single MATLAB statement. It consists of a single MATLAB expression and any number of input and output arguments.

You can define an anonymous function right at the MATLAB command line or within a function or script.

This way you can create simple functions without having to create a file for them.

The syntax for creating an anonymous function from an expression is

Example

In this example, we will write an anonymous function named power, which will take two numbers as input and return first number raised to the power of the second number.

Create a script file and type the following code in it −

When you run the file, it displays −

Primary and Sub-Functions

Any function other than an anonymous function must be defined within a file. Each function file contains a required primary function that appears first and any number of optional sub-functions that comes after the primary function and used by it.

Primary functions can be called from outside of the file that defines them, either from command line or from other functions, but sub-functions cannot be called from command line or other functions, outside the function file.

Matlab with labview

Sub-functions are visible only to the primary function and other sub-functions within the function file that defines them.

Example

Let us write a function named quadratic that would calculate the roots of a quadratic equation. The function would take three inputs, the quadratic co-efficient, the linear co-efficient and the constant term. It would return the roots.

The function file quadratic.m will contain the primary function quadratic and the sub-function disc, which calculates the discriminant.

Create a function file quadratic.m and type the following code in it −

Matlab

You can call the above function from command prompt as −

MATLAB will execute the above statement and return the following result −

Nested Functions

You can define functions within the body of another function. These are called nested functions. A nested function contains any or all of the components of any other function.

Nested functions are defined within the scope of another function and they share access to the containing function's workspace.

A nested function follows the following syntax −

Example

Let us rewrite the function quadratic, from previous example, however, this time the disc function will be a nested function.

Create a function file quadratic2.m and type the following code in it −

You can call the above function from command prompt as −

MATLAB will execute the above statement and return the following result −

Private Functions

A private function is a primary function that is visible only to a limited group of other functions. If you do not want to expose the implementation of a function(s), you can create them as private functions.

Private functions reside in subfolders with the special name private.

They are visible only to functions in the parent folder.

Example

Let us rewrite the quadratic function. This time, however, the disc function calculating the discriminant, will be a private function.

Create a subfolder named private in working directory. Store the following function file disc.m in it −

Create a function quadratic3.m in your working directory and type the following code in it −

You can call the above function from command prompt as −

Matlab With Key

MATLAB will execute the above statement and return the following result −

Global Variables

Matlab With Applications

Global variables can be shared by more than one function. For this, you need to declare the variable as global in all the functions.

Matlab With Arduino

If you want to access that variable from the base workspace, then declare the variable at the command line.

The global declaration must occur before the variable is actually used in a function. It is a good practice to use capital letters for the names of global variables to distinguish them from other variables.

Matlab With Excel

Example

Let us create a function file named average.m and type the following code in it −

Create a script file and type the following code in it −

When you run the file, it will display the following result −

Matlab With Excel