Configure the Run Button for Functions in MATLAB


In this article, we will learn to configure the Run button for functions in MATLAB.

In MATLAB, the `Run` button is used execute a MATLAB code. Also, MATLAB allows us to configure the `Run` button to execute a specific user defined function.

Algorithm to Configure Run Button

The step−by−step process to configure the Run button to execute a specific function in MATLAB is explained below:

Step 1 − Firstly, create a new script to open the MATLAB editor.

Step 2 − Define or create a function for which you want to configure the Run button.

For example, we create a function called `MyFun` to perform addition of two numbers.

function s = MyFun(x, y)
	s = x + y;
end

Step 3 − Now, from the MATLAB editor menu, click on the `Editor` tab and then select “Run”, it will show a list of commands available for running the function.

Step 4 − Click on the last option of the list and replace the text “type code to run” with your function along with the input arguments.

In our case, we will type the following:

s = MyFun(x, y)

This code will run the function `MyFun` with two input arguments `x` and `y`, and stores the result in a variable `s`.

Once, we done this, MATLAB will replace the default Run command with the newly added Run command.

Now, when we click on the `Run` button, MATLAB will execute the newly defined function.

After completing the configuration of the Run button for the function. Try to execute some MATLAB codes with the same function.

For example, if we run the following MATLAB program, we get the result according to the defined function.

% MATLAB Program to execute the `MyFun` function
% Specify the value of input arguments
x = 10;
y = 15;
% Call `MyFun` function
s = MyFun(x, y);
disp('The sum of x and y is:');
disp(s);

Output

The sum of x and y is:
    25

Let us consider some more examples to configure the `Run` button for specific functions.

Example (1)

% MATLAB code to configure the Run button for a function
function p = ElectPower(v, i)
	p = v * i;
end

After configuring the `Run` button for the above function `ElectPower`, try to execute the following MATLAB code.

Example

% MATLAB code to execute the `ElectPower` function
% Specify the values of input arguments
v = 220;
i = 5;
% Call the `ElectPower` function
p = ElectPower (v, i);
disp('The electrical power consumed is:');
disp(p);

Output

The electrical power consumed is:
        1100

Example (2)

% MATLAB code to configure the Run button for a function
function r = resistance(v, i)
	r = v/i;
end

After configuring the `Run` button for the above function `resistance`, try to execute the following MATLAB code.

Example

% MATLAB code to execute the `resistance` function
% Specify the values of input arguments
v = 12;
i = 3;
% Call the `resistance` function
r = resistance(v, i);
disp('The electrical resistance is:');
disp(r);

Output

The electrical resistance is:
     4

Conclusion

Hence, this is all about configuring the Run button for functions in MATLAB. MATALB allows us to configure the Run button to call a specific function which makes the testing and debugging of individual functions easier without executing the entire script in the MATLAB program.

Updated on: 07-Aug-2023

34 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements