Matlab Quickguide

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Operators and Special Characters Matrices and Mathematics Loops and Statements

+ - * / ^ Mathematical operators a:b:c From a to c by b for i = a:b For loop


.* ./ .^ Elementwise operators linspace() Evenly-spaced array actions
end
pi 3.14159... sum() Sum of array while <true> While loop
NaN Not a Number mean() Average of array actions
Inf Infinity range() Range of array end
[ ] Create a matrix size() Dimensions of matrix switch <variable> Switch statement
x = [1, 2; 8, 10] max() case <value 1>
Max value of array
actions
( ) Index into matrix min() Min value of array case <value 2>
x(1, 2) = 2 abs() Absolute value actions
Order precedence log() Natural log otherwise
… Line continuation log10() Base 10 log actions
, sqrt() end
Separate lines or Square root if <condition> If statement
elements of matrix e Scientific notation (7e2) actions
; Denote new line and cos() Trigonometric functions elseif <condition>
suppress output sin() actions
% Comment tan() else
’ exp() e to the power actions
Transpose matrix
round() Round value end
‘ ’ Define character
ceil() break Ends loop
“ ” Define string Round up
floor() continue Begin next loop
= Variable assignment Round down
fix() Round towards zero iteration
== Equality
~= rref() Solve linear equations
Not equal to
> >= det() Calculate determinant
Greater than / equal to Command Line & Setup
< <= eye() Identity matrix
Less than / equal to clc Clear command window
& zeros() Zero matrix
And clearvars Clear variables
&& rand() Random value 0 to 1
Short-circuit And clear all Clear everything
| randi() Random discrete value
Or close all Close all figure windows
|| Short-circuit Or format Compact output
compact
Variables & Datatypes who List workspace variables
double() Convert to double whos
Inputs and Outputs
display() string() Convert to string help Display help and notes
Output to command line <function>
input() char() Convert to character for function
Prompt user input
fprintf() logical() Convert to logical version Display MATLAB version
Format output
xlsread() true Boolean true (1) why Philosophical answers
Read excel file
xlswrite() false Boolean false (0)
Write excel file
readtable() Read .csv file
table2array() Convert table to array
Plotting Commands Plotting Line Styles
plot()
plot3()
loglog()
semilogx()
2D plot
3D plot
Log plot
Log plot (x-axis)
-
--
:
-.
Solid line (default)
Dashed line
Dotted line
Dash-dot line
MATLAB
semilogy() Log plot (y-axis)
meshgrid() Generate meshgrid
surf() Surface plot Plotting Shapes
scatter() . Point
Scatter plot
histogram() o Circle
Histogram
pie() x X-mark
Pie chart
barplot()
colorbar()
Bar plot
Add colorbar
+
*
Plus
Star
Syntax Guide
xlabel() s Square
Add axis title
ylabel() d Diamond
xlim([ ]) Axis plot limits v Triangle (down)
ylim([ ]) ^ Triangle (up)
title() Add graph title < Triangle (left)
legend() Add labeled legend > Triangle (right)
text() Add text at coordinates p Pentagram
gtext() Add text with click h Hexagram
grid on Add grid to plot
hold on Plot more data on graph
figure() Create new figure Sample Function .m File
subplot() Specify subplot on figure calcRMSE.m % Calculate RMSE for
polyfit() Data
Obtain fitted coefficients
function [RMSE] =
polyval() Calculate fitted y-data calcRMSE(y, y_fit)

% Calculate Root Mean


Square Error
Plotting Colors a = y – y_fit;
r Red a2 = a.^2;
b Blue a3 = mean(a2);
RMSE = sqrt(a3);
g Green end
c Cyan Sample Anonymous Function
m Magenta calcVolSphere = @(r) 4/3*pi*r^3
y Yellow
k Black
w White

You might also like