You are on page 1of 3

%% Basic commands in Matlab

% By Wenbin Wang

%% Getting started with Matlab


clc; % Clear command window
clear; % Clear variables and functions from memory

% ; is to suppress the output of code line

%% Ask for help


help clc; %Help with functions in command window
doc clear; %same to help but in browser

% You may need more functions in your homework and lab reports. You can use
% these two tools to help you understand your functions better.

%% MATLAB as a calculator
3 + 5;
5 * 3;
18 ./ 5;
1e-3 % Scientific notation
sin(10) % Sin, in radians
3*sqrt(9) % Square root
2.^2 % Squared
exp(3) % Exponential

%% format
format long % Scaled fixed point format with 15 digits
5./2
pi
format short % Scaled fixed point format with 5 digits
5./2
pi

% Notice that Matlab echos the results, which are saved in a variable name
"ans"
%% variables
a = 10; % You can name your own variables
b = 5;
c = a + b

who % List current variables

% Reserved keywords
a = 1 + 2i % Complex number
i % Imaginary unit
j % Same to i
pi

%% Vector and Matrix


a = [1 2 3; 4 5 6; 7 8 10]
b = [1, 1, 1; 1, 1, 1; 1, 1, 2]
aT = a' % Transpose of a matrix
aT = transpose(a) % Another way to do transpose of a matrix
a1 = reshape(a,1,9) % Reshape a to [1, 9]
a2 = rot90(a) % Rotate a by 90 degrees
c = a*b % Matrix multiplication

% Component-wise, component by component


c = a.*b
c = a./b
c = a.^b

% Manipulate a matrix
a(3) % The third element of a
a(:) % All emelents in a
a(:,:) % All elements in a
a(:, 1:2) % Take the 1st and 2nd columns
a(1:2, 1:2) % The 1st and 2nd columns and 1st and 2nd rows
a(2, end) % Last element in the second row
a(1:2:3, end-1) % The second to the last elements in the 1st & 3rd row

%% functions for matrix


min(a) % If a is a vector, return minimum value in a;
% If a is a matrix, return minimum values in each column
max(a) % If a is a vector, return maximum value in a;
% If a is a matrix, return maximum values in each column
mean(a) % If a is a vector, return mean of a;
% If a is a matrix, return mean of each column
size(a) % Size of a
length(a) % Length of a vector

%% Functions to creat a matrix


A = ones(3) % Return a matrix of ones.
ones(4, 2)
zeros(2) % Return a matrix of zeros.
B = magic(3) % Return a magic square
rand(5) % Random 5-by-5 matrix
cat(1, A, B) % Concatenate arrays

%% Plot
figure; % Create figure window
x = linspace(1, 5, 100); % Give a linspaced vector.
% An alternative way
x_test = 1:0.05:5;
% try x = 1:5;
y = sin(x);
plot(x, y); % Plot y(x)
xlabel('x'); % Label x
ylabel('sin'); % Label y
% xlabel('x_2') % Subscripts
% xlabel('\theta') % Greek letters
title ('Sine function'); % give a title
legend('sin'); % Legend
grid on; % Turn on grid

%% Plot another curve on existing figure


hold on; % Hold existing figure
y2 = cos(x); % Give another
plot(x, y2, 'b*'); % plot y2 with blue asterisk

%% Close existing figures


close all
%% Plot two curves at one time
plot(x, y, 'bo-', x, y2, 'r*');
legend('sin', 'cos');
xlabel('\theta (rads)');
ylabel('Cosine and Sine of \theta');
title('Trigonometric functions');
%% One figure with several subplots
subplot(1,2,1) % Start a subplot.
plot(x, y, 'r')
title ('Sine function');
xlabel('\theta (rads)');
ylabel('Sine of \theta');

subplot(1, 2, 2)
plot(x, y2, 'b');
title ('Cosine function');
xlabel('\theta (rads)');
ylabel('Cosine of \theta');

%% Import a txt or xls or cvs file


% There are several methods for importing a file into Matlab.
% One of the easist way is to use "Import Data" under "Home" tab.
% a. Click Import Data
% b. Select, and then open your file
% c. Click the triangle under Imported Data, and select your output type,
% for example, "Numeric Matrix". If you choose any other output type, you
% may need extra functions to manipulate your data.
% d. Then you can manipute it with the functions you have learned above.

%% This is the End. Enjoy!

You might also like