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.