MATLAB Scripts

Scripts Introduction

Matlab Script represents some programs and is executed like we execute command in the command window of Matlab’s environment. Matlab script is also defined as a sequence of commands; using the ‘edit’ command in Matlab, we create scripts directly by selecting option from the menu bar. Variables which are created during each session in a script are visible in the workspace section and can be accessed from the command window till we terminate the session. We must save it in the current directory on the Matlab path to execute and run any script file. ‘.m’ extension is used to save scripts in Matlab and referred to as "M-files".

Example:

here p and b1 are added and stored in variable s to use it further.

p = 5; b1 = 7;
s = p + b1
d2 = s + sin(b1)
e3 = 5 * d2
f1 = exp(-d2)

output:

When the above script file is saved, and executed from command line of Matlab, it produces the following output -

s =  12
d2 =  12.657
e3 =  63.285
f1 =    3.1852e-06

What is a script?

Scripts are the difficult kind of program document. For computerizing a progression of MATLAB® orders or commands, scripts are helpful. For example, calculations that you need to perform more than once from the command line or arrangement of commands you need to reference.

How to create a script?

There are two ways creating a script

1. using the ‘edit’ command in Matlab command line

MATLAB Scripts

2. We can also create scripts directly by selecting option "new script" from the home menu bar.

MATLAB Scripts

Basic functions

Functions

Edit(): to edit or create new files.

Grabcode(): to extract a Matlab code file from file published.

Input(): to initiate a request user input from user.

Publish(): to generate a new view of MATLAB file.

Snapnow(): To take snapshot of image in published document.

Let us understand through examples

Matlab Programming logic Examples give you a short outline of Matlab programs. Understudies should know about Matlab programming to carry out a total undertaking in Matlab. Mathworks have been additionally working with Matlab for as far back as eleven years. Taking a task in Matlab is the most ideal approach to learn Matlab programming. We can offer matlab programming models for you from a great many activities as we have grown almost 9000+ undertakings in Matlab. By and large, Matlab programming should be possible utilizing object-situated programming, Graphic User Interface programming, and fundamental Matlab language structure and capacities. In the event that you need any projects or undertakings in Matlab, mail us your necessities, we will give you your necessary code. We have dealt with each variant of Matlab, including the most recent adaptation.

Our Examples gives a short and clear understanding about Matlab programming nuts and bolts. The rudiments of Matlab are fundamental, to find out about Matlab programming. We additionally support progressed and complex projects in Matlab for understudies who help in our assistance. These all are essential Matlab ideas that one needs to know prior to taking complex projects. We have counted beneath, not many model projects on capacities, designs, and number juggling capacities. We have made easy to answer for each issue.

1.edit

We are Creating two new files in a new folder called "newmydir".

In new directory called "newmydir" file, is created using a character vector. Next moment a dialog box then appears, if you want to create newmyscript.m. Then we open tests/newmyscript.m.

mkdir newmydir
edit newmydir/newmyscript.m

Create a second file using a string. Click Yes to create and open tests/new_script2.m

t = "newmydir/newmyscript2.m";
edit(t)
MATLAB Scripts

2. input

p = input(promts user) this waits for the user or programmer to enter a value and waits for user to click return key . The user or programmer can enter any expressions like rand(4),ones(2).

  • If the Return key is pressed by user then without entering anything, then an empty matrix is returned.
  • If an invalid expression is entered at the prompt, then an relevant error message is displayed, and then the prompt is redisplayed again to the user.

Example:

prompt = 'enter the original value please? ';
p = input(prompt)
o = p*20

Output:

MATLAB Scripts

3.publish

publish generates a temporary view of the file passed as argument to the function publish() and output is generated as an HTML file format that can be helpful for many purpose like sharing. The HTML file is saved by publish and a file is saved and creates a file named html in subfolder. The html file is located in subfolder is the location of file.

Example:

Filename1 = fullfile(matlabroot,'help','techdoc','matlab_env','examples','fourier_demo2.m');
copyfile(filename,'.','f')
%publish function to run the example and generate an HTML view of the example. 
publish('fourier_demo2.m');
web('html/fourier_demo2.html')

Output:

MATLAB Scripts

4.grabcode

grabcode function copies the code from the HTML file (mentioned filename) and then it tries to paste it in the Editor. To get MATLAB code from any internet published files use grabcode, when no availability of the source code is not ready. In MATLAB, code appears as HTML comments at the last of filename from the file mentioned.

Example:

mycode = grabcode('my_sine_wave.html')

Output:

MATLAB Scripts

5.snapshot

This function takes snapshot of the image in published document for later inclusion use.

We will look out the following example to understand better.

Example:

%% we are Scaling magic Data and viewed as Image
for o=1:4
    imagesc(magic(o))
end
publish('loopIterations.m')
web('html/loopIterations.html')

Output:

MATLAB Scripts

Basic programs

1.Random numbers script file

We have written the code with explanation in the comments embedded in the code

Steps followed in the example:

  1. we are first Initializing the number of columns for representation.
  2. we are also Initializing the number of rows for representation.
  3. we are now defining the bins for the histogram presentation.
  4. we are using the ‘Random number generator’ to generate random values for representation.

Example:

Code:

Colno= 4000;
%we are Initializing the number of columns for representation
Rowno = 1;
%[we are Initializing the number of rows ]
bins = Colno/100;
%[we are Defining the bins for the following presentation]
rng(now);
% create random values for representation]
P = 100*rand(Row, Col);
histogram(P, bins)
MATLAB Scripts

2.To draw a sphere, we will create a script file

Steps to draw a sphere in Matlab environment, this example:

1.To create the new script we use ‘edit’ command

2.Next we build the logic of sphere to write a Matlab code.

Example:

Code explanation:

edit spherediag

[ The command ‘edit’ is used to create new script in the directory named ‘spherediag’]

 [e, d, c] = sphere;

[we are creating a unit sphere]

Rad = 2;

[the radius of the sphere is initialized]

Surf (e * Rad, d * Rad, c * Rad)

[Adjusting the dimensions of the required sphere]

axis equal

[Making the scale of each axis common] [this file is saved as .m extension]

Next, run this script file for executing, only the name of the file is typed in the command window as follows:

spherediag

code

edit drawSphere
[e, d, c] = sphere;
Rad = 3;
surf(e * Rad, d * Rad, c * Rad)
axis equal

Output:

MATLAB Scripts

3. To Plot the Graph a=p2 

Here we are initializing p and obtaining a graph which is equal to this equation a=p2.

We have added title to our graph "Graph of a=p^2", also we have added two other labels such as blabel('p') and alabel('a') to make our graph look more appealing. We have used this commands to add more characteristics to the existing above plot.

Code:

p = -1:0.1:1
 a= p.*p
 plot(a,p)
title('Graph of a=p^2')
 blabel('p')
alabel('a')

Output:

p =
  Columns 1 through 15
   -1.0000   -0.9000   -0.8000   -0.7000   -0.6000   -0.5000   -0.4000   -0.3000   -0.2000   -0.1000         0    0.1000    0.2000    0.3000    0.4000
  Columns 16 through 21
    0.5000    0.6000    0.7000    0.8000    0.9000    1.0000
a =
  Columns 1 through 15
    1.0000    0.8100    0.6400    0.4900    0.3600    0.2500    0.1600    0.0900    0.0400    0.0100         0    0.0100    0.0400    0.0900    0.1600
  Columns 16 through 21
    0.2500    0.3600    0.4900    0.6400    0.8100    1.0000
MATLAB Scripts

Conclusion

Scripts in Matlab comprise of a grouping of commands or orders which together constitute as a program, and by calling their file name from a different command window the scripts are executed. While making a content, we should save it as .m extention, we refer this as M-files and keep the record name same as the name of the content.


Related Topics

MATLAB Tutorial

Introduction of MATLAB In this MATLAB(stands for matrix laboratory) tutorial, we will cover core concepts of MATLAB and provide a solid foundation to enhance high computing skills. We are going to...

4 minutes read.

MATLAB Program - To Solve Differential Equation Using Euler’s Method

Using Euler's method to solve differential equation Can you imagine a point where we do not use differential equation to solve differential condition? Truth be told, there are a few different...

3 minutes read.

Matlab Unit impulse signal generation

What is the Necessity of learning Matlab programs? Matlab Programming Examples give you an idea of typical Matlab programs in a nutshell.Matlab programming should be utilizing object-oriented programming, GUI programming, and...

3 minutes read.

Matlab Graphic image

Matlab’s Graphic image Images are stored as arrays of numeric data in MATLAB® environment, so you can perform various sort of analysis on them to better visualize it. Picture Processing applications are given...

3 minutes read.

MATLAB Strings

You can address text in MATLAB utilizing string exhibits. Every component of a string exhibit stores a grouping of characters. The arrangements can have various lengths without cushioning, for example,...

5 minutes read.

MATLAB vs Other Languages

MATLAB vs Other Languages If we try to find which is the best computer programming languages, the answer is not straightforward, and that might sound similar to asking which is the...

3 minutes read.

matlab polynomials

In mathematics, a polynomial is an articulation comprising of indeterminates factors and the respective coefficients, that includes just the tasks of augmentation, expansion, deduction, and non-negative whole number exponentiation of...

4 minutes read.

Matlab Square Wave Signal Generation

What is Square Wave signal Generator? A “square wave” is a sort of non-sinusoidal waveform, most normally experienced in gadgets and sign preparing. An optimal square wave substitutes consistently and momentarily...

3 minutes read.

MATLAB Loops

Loops in programming Concept of looping is used to execute instructions repeatedly. It helps coders to reduce lines of code by setting a condition. In looping first a condition is fixed,...

3 minutes read.

MATLAB Control Statements

What are control statements? The name itself suggests that something is being controlled, these control statements decide or alter the flow of execution ina program. In program, we often need to...

3 minutes read.

MATLAB Features

MATLAB Features MATLAB (abbreviation for matrix laboratory) is an interactive programming environment that eases high-power computations in the IT world. Initially, it was developed with the intention of matrix calculation only....

4 minutes read.

MATLAB Butterworth filter

What is Butterworth filter? The Butterworth channel is a kind of sign preparing channel intended to have a recurrence reaction as level as conceivable in the passband. It is likewise alluded...

3 minutes read.

MATLAB Variables

What are variables? A variable is a storage location or a container to store or hold data. In programming, variables are used to reservedata created by the programmers so that it...

4 minutes read.

Matlab function

Introduction: Functions in MATLAB are composed with code that link one variable with another, and yielded output is connected precisely to one specific information that shapes a significant piece of any...

6 minutes read.

Differentiation in Matlab

Differentiation in Mathematics In maths, Derivatives are an essential device of math or calculus. For instance, derivative of the situation of a moving article regarding time is the item's speed: this...

6 minutes read.

MATLAB Commands

MATLAB Commands MATLAB (stands for matrix laboratory) is a high-level programming environment for scientific calculation, and it provides functions more efficiently than other programming languages. In addition, it has many in-build...

3 minutes read.

matlab Integral

What is integration? In maths, Integral allots numbers to functions in a manner that portrays relocation, region, volume, and different ideas that emerge by consolidating tiny information. The way toward discovering...

5 minutes read.

Matlab Calculus

Analytics Calculus is significant in various fields like designing, physical science, and different sciences, yet the estimations are finished by machines or PCs, regardless of whether they are into Physics...

3 minutes read.

MATLAB Operators

What is an Operator? A specific operation is performed by using a symbol which is called an operator. Example: A+B, where A, B are called operand and '+' is called operator. If you...

8 minutes read.

MATLAB Scripts

Scripts Introduction Matlab Script represents some programs and is executed like we execute command in the command window of Matlab’s environment. Matlab script is also defined as a sequence of commands;...

6 minutes read.