Show Sidebar Hide Sidebar

# Basic Shapes in MATLAB

How to make a Basic Shapes in MATLAB. Examples of different shapes.

#### Plot A Circle with Given Radius

``````% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

fig = figure;
hold on
th = 0:pi/100:2*pi;
xunit = r * cos(th);
yunit = r * sin(th);
plot(xunit, yunit);
hold off
axis square;

resp = fig2plotly(fig, 'strip',false);
plotly_url = resp.url;``````

#### Plot A Unit Circle in Complex Plane

``````% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

% generate Complex Number:

theta = 0:pi/100:2*pi;
r = 1
z = r*cos(theta) + i*r*sin(theta)

% Plot data
fig = figure;
plot(z);

% Convert and send to plotly
resp =fig2plotly(fig)
plotly_url = resp.url
``````

#### Plot A Simple Ellipse Equation

``````% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

t=0:0.1:2*pi;
x=2*cos(t);
y=3*sin(t);
plot(x,y);
grid on;
response = fig2plotly;
plotly_url = response.url;``````

#### Plot A Covariance Ellipse

``````% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

%# generate data
num = 50;
X = [ mvnrnd([0.5 1.5], [0.025 0.03 ; 0.03 0.16], num) ; ...
mvnrnd([1 1], [0.09 -0.01 ; -0.01 0.08], num)   ];
G = [1*ones(num,1) ; 2*ones(num,1)];
fig = figure;

gscatter(X(:,1), X(:,2), G)
axis equal, hold on

for k=1:2
%# indices of points in this group
idx = ( G == k );

%# substract mean
Mu = mean( X(idx,:) );
X0 = bsxfun(@minus, X(idx,:), Mu);

%# eigen decomposition [sorted by eigen values]
[V D] = eig( X0'*X0 ./ (sum(idx)-1) );     %#' cov(X0)
[D order] = sort(diag(D), 'descend');
D = diag(D);
V = V(:, order);

t = linspace(0,2*pi,100);
e = [cos(t) ; sin(t)];        %# unit circle
VV = V*sqrt(D);               %# scale eigenvectors
e = bsxfun(@plus, VV*e, Mu'); %#' project circle back to orig space

%# plot cov and major/minor axes
plot(e(1,:), e(2,:), 'Color','k');
%#quiver(Mu(1),Mu(2), VV(1,1),VV(2,1), 'Color','k')
%#quiver(Mu(1),Mu(2), VV(1,2),VV(2,2), 'Color','k')
end

resp = fig2plotly(fig)
``````
Inspired from Stack Overflow
Still need help?