EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial Matlab Scripts
 

Matlab Scripts

Priya Pedamkar
Article byPriya Pedamkar

Updated March 4, 2023

Matlab Scripts

 

 

Introduction to Matlab Scripts

Matlab Script is a sequence of various commands, which are most likely used to represent some program and are executed in the same way as a program or single command in Matlab command window. The script is created using ‘edit’ command in Matlab. Variables that are created in a script can be accessed from the Matlab command window until we clear them or terminate the session. To run our script, we must save it in current directory, or in a directory saved on Matlab path. Matlab scripts must be saved as ‘.m’ extension and this is the reason they are referred as “M-files”.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Examples of Matlab Scripts

Given below are the examples:

Example #1

In this example we will create a script that will generate 5000 random numbers between 0 and 100. We will also create a histogram for all these numbers.

Below are the steps that we will follow for this example:

  • Use ‘edit’ command to create new script.
  • Write the code for generating 5000 random numbers and drawing a histogram.

Code:

edit myHisto

[Using the ‘edit’ command to create the script ‘myHisto’]

Col= 5000;

[Initializing the number of columns]

Row = 1;

[Initializing the number of rows]

Bins = Col/100;

[Defining the Bins for the histogram]

rng(now);

[Using the ‘Random number generator’ to create random values]

A = 100*rand(Row, Col);

histogram(A, Bins)

[Drawing the histogram using above values] [Save this file as .m extension. Please keep in mind that the name of the file must be same as the name of the script, which is ‘myHisto’ in our example]

Next, we need to call this script. This is done by typing the name of the script in the command window as below:

myHisto

[Calling the script created in the command window]

Input:

edit myHisto
Col= 5000;
Row = 1;
Bins = Col/100;
rng(now);
A = 100*rand(Row, Col);
histogram(A, Bins)

[Please notice that in the above figure, the name of the file is same as the name of the script]

Output:

Matlab Scripts 1

As we can see in the output, we have obtained a histogram of random values as expected by us.

Example #2

In this example we will create a script that will be used to find the integration of a function.

Below are the steps that we will follow for this example:

  • Use ‘edit’ command to create the new script.
  • Write the code for computing the integration using ‘integral’ function.

Code:

edit myIntegral

[Using the ‘edit’ command to create the script ‘myIntegral’]

syms x

[Initializing the local variable ‘x’]

Fx = @(x) 5*x.^3

[Creating the polynomial function of degree 3]

A = integral (Fx, 0, 3)

[Passing the input function& the required limits] [Save this file as .m extension and keep the name as ‘myIntegral’] [Mathematically, the integral of 5*x ^3, between the limits 0 to 3 is 101.25]

Next, we need to call this script. This is done by typing the name of the script in the command window as below:

myIntegral

[Calling the script created in the command window]

Input:

edit myIntegral
syms x
Fx = @(x) 5*x.^3
A = integral (Fx, 0, 3)

Output:

Matlab Scripts 2

As we can see in the output, we have obtained integration of our function by calling the script.

Example #3

In this example we will create a script that will be used to draw a sphere.

Below are the steps that we will follow for this example:

  • Use ‘edit’ command to create the new script.
  • Write the code for drawing a sphere of radius ‘Rad’.

Code:

edit drawSphere

[Using the ‘edit’ command to create the script ‘drawSphere’] [a, b, c] = sphere;

[Creating unit sphere]

Rad = 2;

[Initializing the radius]

surf(a * Rad, b * Rad, c * Rad)

[Adjusting the dimensions & creating the plot]

axis equal

[Making the scale common for each axis] [Save this file as .m extension and keep the name as ‘drawSphere’]

Next, we will call this script. This is done by typing the name of the script in the command window as below:

drawSphere

[Calling the script created in the command window]

Input:

edit drawSphere
[a, b, c] = sphere;
Rad = 2;
surf(a * Rad, b * Rad, c * Rad)
axis equal

Output:

draw a sphere

As we can see in the output, we have obtained a sphere by calling the script.

Example #4

In this example will plot a sine wave and a cos wave in the same plot using script.

Below are the steps that we will follow for this example:

  • Use ‘edit’ command to create the new script.
  • Write the code for creating the waves and plot them.

Code:

edit drawWaves

[Using the ‘edit’ command to create the script ‘drawWaves’]

A = linspace (-pi, 2*pi);

[Initializing the interval]

Y = sin(A);

[Creating the sine wave]

Z = cos(A);

[Creating the cos wave]

T = plot(A, Y, A, Z);

[Creating the plot] [Save this file as .m extension and keep the name as ‘drawWaves’]

Next, we will call this script. This is done by typing the name of the script in the command window as below:

drawWaves

[Calling the script created in the command window]

Input:

edit drawWaves
A = linspace (-pi, 2*pi);
Y = sin(A);
Z = cos(A);
T = plot(A, Y, A, Z);

Output:

will plot a sine wave and a cos wave in the same plot

As we can see in the output, we have obtained a plot containing sine and cos waves by calling the script.

Conclusion

Scripts in Matlab consist of a sequence of commands which we use as a program by calling them from a separate command window. While creating a script, we must save it as .m extension and keep the file name the same as the name of the script.

Recommended Articles

This is a guide to Matlab Scripts. Here we discuss the introduction to Matlab Scripts along with the examples for better understanding. You may also have a look at the following articles to learn more –

  1. String Array in Matlab
  2. Simplify Matlab
  3. Factorial in Matlab
  4. MATLAB Unique

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW