Professional Documents
Culture Documents
ME 310
Useful Matlab Commands
In this tutorial, some of the useful Matlab commands are listed and basic programming using m-files are
explained. Note that it is not possible to list all commands of Matlab. If another function or command
is required, help commands listed in Section 2 should be used.
1 Basic Commands
ctrl + c : stop the execution of Matlab (press control and c keys simultaneously)
% : comment
2 Getting Help
help : display help topics in command window. General help topics are listed in Table 1. These
keywords are used to list the commonly used functions related to the subjects. Execute help
keyword command to see the related functions
Keyword Description
general General purpose commands
ops Operators and special characters
lang Programming language constructs
elmat Elementary matrices and matrix manipulation
elfun Elementary math functions
specfun Specialized math functions
matfun Matrix functions - numerical linear algebra
polyfun Interpolation and polynomials
scribe Annotation and Plot Editing
graph2d Two dimensional graphs
graph3d Three dimensional graphs
Page 1 of 7
ME310 - Matlab Tutorial
3 Variable Declaration
b = cos(0) ⇒ b = 1
3.2 Vectors/Arrays
a = [1 5 7 8] ⇒ a = 1 5 7 8
b = 1 : 5⇒b = 1 2 3 4 5
3.3 Matrices
1 5
A = [1 5 ; 7 8] ⇒ A =
7 8
linspace(x1,x2,N) : generates N equally spaced points between x1 & x2
transpose(A) or A’ : transpose of A
Element extraction from a matrix is similar to vectors. As an example consider the following commands.
>> A(2,3) % extracting the element of A at the 2nd row and 3rd column
ans =
7
Page 2 of 7
ME310 - Matlab Tutorial
10
Matlab software has two different types of arithmetic operations as matrix operations and element
by element (array) operations. These two operations are distinguished by period (.) character.
hold on/off : hold current plot in order to add more graph to this plot
5 Script/Function Development
In Matlab, a convenient way of developing programs is using m-files instead of command window. edit
command opens the m-file editor. Scripts or functions can be develop by this editor. Scripts are pro-
grams, which do not require any inputs. In addition, they cannot give any output at the end of the
program. However, in general, functions need some input parameters and can output some results to
Page 3 of 7
ME310 - Matlab Tutorial
other programs. Another important difference is that, the variables in scripts are not cleared unless spec-
ified by the user. On the other hand, declared variables in functions are removed after the termination
of the function. In order to define a function following syntax should be used.
This function should be saved in the working directory of Matlab with the function name in order to run
properly. Then, fun1 can be called similar to built-in functions in Matlab.
It is possible to write more than one functions in an m-file. In this case, the upper function becomes
the main function and it can be called from command window. However, the remaining functions below
become local functions and cannot be called outside this m-file.
Here, main function fun1 can be called from command window and from other m-files. But sub-functions,
fun2 & fun3 can only be used in this m-file. fun1 can use these functions and fun2 and fun3 can also call
each other. These sub-functions cannot be used by other m-files. Because these functions are cleared
once the main function is terminated similar to other local variables in a function. Note that the name
of this m-file should be fun1.m in order to run properly.
1. == : equal to
2. >, >= : greater than, greater than or equal to
3. <, <= : less than, less than or equal to
Page 4 of 7
ME310 - Matlab Tutorial
% Asks for user input and assign the given value to variable a
>> a = input(['Please enter a number : '])
Please enter a number : 5.5
a =
5.5000
% script
a = input('Enter a number to calculate its square root: ');
b = sqrt(a);
disp(['the square root of ' num2str(a) ' is ' num2str(b)]);
% output
Enter a number to calculate its square root: 20
the square root of 20 is 4.4721
Note that, in order to display a numerical variable using disp command, this number should be converted
to a ‘string’. This conversion be performed by num2str() function.
Permission Description
‘r’ read
‘w’ write (create if necessary)
‘a’ append (create if necessary)
Page 5 of 7
ME310 - Matlab Tutorial
% Opening input and output files. Note that these files must be
% in the working directory of Matlab. Otherwise full path should be defined
% e.g. : input file = fopen('D/temp/input.txt','r');
input file = fopen('input.txt','r');
output file = fopen('output.txt','w');
for i = 1:size(column1,1)
% here arrays are printed to the file using a for loop. it is possible to
% directly write an array or a matrix. check help document of fprintf function
fprintf(output file,'\n %f \t %.2f \t %.4f',column1(i),column2(i),column3(i));
end
% closing files
fclose(input file);
fclose(output file);
input file :
0.8147 0.0975 0.1576
0.9058 0.2785 0.9706
0.1270 0.5469 0.9572
0.9134 0.9575 0.4854
0.6324 0.9649 0.8003
output file:
col 1 col 2 col 3
0.814700 0.10 0.1576
0.905800 0.28 0.9706
0.127000 0.55 0.9572
0.913400 0.96 0.4854
0.632400 0.96 0.8003
>> g = inline('xˆ2+3*x+5')
g = Inline function:
g(x) = xˆ2+3*x+5
>> g(3)
ans =
23
Page 6 of 7
ME310 - Matlab Tutorial
tic/toc : measure elapsed time between them. Write tic at the beginning of a script and toc
at the end to measure the execution time of the script.
Page 7 of 7