Professional Documents
Culture Documents
Vinod Saini
Outline
Day-3
Debugging
Useful Instructions
try- catch and eval function
Techniques to Improve Performance
3D Plotting
Curve Fitting
Symbolic Toolbox
GUI
Debugging
Set Breakpoints
to pause the execution of a MATLAB file so you can examine the value or variables
Debugging
Clear Breakpoint
Clear Breakpoint in all the files
Step
Step in
Step out
Continue
Exit debug
Working directory
Debugging
[M, N] = size(X);
isnan(X)
for matrix X, returns the number of rows and columns in X as separate output variables.
N = size(X, 2) - number of columns
M = size(A,1) number of rows
returns an array that contains 1's where the elements of X are NaN's and 0's where they
are not. For example, isnan([pi NaN Inf -Inf]) is [0 1 0 0].
isinf(X)
clearvars X*
disp(X)
abs(X)
ls
Absolute value of X
isempty(X)
pwd
mkdir
rmdir
rmdir(folderName) removes the folder folderName from the current folder if folderName
is empty
cd / cd..
who
who lists in alphabetical order the names of all variables in the currently active
workspace.
who -file filename lists the variable names in the specified MAT-file.
what
what lists the path for the current folder and all the MATLAB relevant files and folders
found in the current folder
csvread
M = csvread(filename,R1,C1) reads data from the file starting at row offset R1 and column
offset C1.
csvwrite
xlswrite
xlsread
Plotting Functions
surf /mesh
surf(X,Y,Z) uses Z for the color data and surface height. X and Y are vectors or matrices
defining the x and y components of a surface.
Plot of sinc (z=sin(r)/r )function
[X,Y] = meshgrid(-8:.5:8); % [X,Y] = meshgrid(x,y) returns 2-D grid coordinates based on the
coordinates contained in vectors x and y
R = sqrt(X.^2 + Y.^2) + eps;
Z = sin(R)./R;
figure
mesh(Z)
figure
surf(X,Y,Z)
Curve Fitting
Data Types
char
Character array
Str = Hello; Str = [Hello; Everyone;];
k = findstr(str1, str2)
Data Types
cell
To be used when you want to have different types of data types (char, integer)
Useful when you are writing an excel file
C = cell(dim) => create cell array of dimension dim
A = cell2mat(C) => convert cell to matrix
mat2cell, num2cell
struct
Data Type
Employee
Employee(1)
Employee(1).salary
10
Syntex
Try
Statements
catch exception
Statements
end
You can't use A./B : works only if A and B are of same length (./ element wise division)
eval function
Syntex
eval(EXPRESSION)
evaluates the MATLAB code in the string EXPRESSION.
Use
eval function
edit evalexample.m
number_of_variables = 10;
for i = 1:number_of_variables
str = ['X_' num2str(i) '=' num2str(i) ';'];
% str = 'X_1 = 1;'
eval(str)
end
X = zeros(20,1) or X = nan(20,1)
Symbolic Toolbox
syms a b c x
f = a*x^2 + b*x + c;
syms f(x, y) : This syntax creates the symbolic function f and symbolic variables x and y
f(x, y) = x^2 + y^2
f(2,2)
Symbolics Toolbox
Differentiation
syms x;
f(x) = [x x^2; x^3 x^4];
f(2)
syms x y a b c f
f=a*x^3+b*x+c*y^2
diff_fx = diff(f,x); diff_fy = diff(f,y)
simple(diff_f) => if the differentiation looks complex
Integration
int_f = int(f,x)
int_f = int(f,x,-1,1) (definite integrals )
Symbolics Toolbox
Substitution
Solving Equation
References