Friday, 5 August 2016

MATLAB Programming 50 - bar charts (Example)

Specify the bar locations along the x-axis


x = 1900:10:2000;
y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
bar(x,y)


Set the width of each bar to 40 percent of the total space available for each bar.


y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
bar(y,0.4)




Display four groups of three bars


y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
bar(y)




MATLAB Programming 50 - bar charts


Drawing Bar Charts


The bar command draws a two dimensional bar chart. Let us take up an example to demonstrate the idea.

Example


Let us have an imaginary classroom with 10 students. We know the percent of marks obtained by these students are 75, 58, 90, 87, 50, 85, 92, 75, 60 and 95. We will draw the bar chart for this data.


Create a script file and type the following code:


y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
bar(y)


When you run the file, MATLAB displays the following bar chart:





Thursday, 4 August 2016

MATLAB Programming 49 - adding some functionality to graph (3)


Setting Colors on Graph


MATLAB provides eight basic color options for drawing graphs. The following table shows the colors and their codes:

w- White
k - Black
b - Blue
r - Red
c - Cyan
g - Green
m - Magenta
y - Yellow

Example


Plot three sine curves with a small phase shift between each line. Use a green line with no markers for the first sine curve. Use a blue dashed line with circle markers for the second sine curve. Use only cyan star markers for the third sine curve.



x = 0:pi/10:2*pi;
y1 = sin(x);
y2 = sin(x-0.25);
y3 = sin(x-0.5);

figure
plot(x,y1,'g',x,y2,'b--o',x,y3,'c*')




MATLAB Programming 49 - adding some functionality to graph (2)


Drawing Multiple Functions on the Same Graph


You can draw multiple graphs on the same plot. The following example demonstrates the concept:

Example


Create a script file and type the following code:


Define x as 100 linearly spaced values between $-2\pi$ and $2\pi$. Define y1 and y2 as sine and cosine values of x. Create a line plot of both sets of data.

x = linspace(-2*pi,2*pi);
y1 = sin(x);
y2 = cos(x);

figure
plot(x,y1,x,y2)


MATLAB Programming 49 - adding some functionality to graph (1)


Adding Title, Labels, Grid Lines, and Scaling on the Graph


MATLAB allows you to add title, labels along the x-axis and y-axis, grid lines and also to adjust the axes to spruce up the graph.

The xlabel and ylabel commands generate labels along x-axis and y-axis.

The title command allows you to put a title on the graph.

The grid on command allows you to put the grid lines on the graph.

The axis equal command allows generating the plot with the same scale factors and the spaces on
both axes.

The axis square command generates a square plot.



MATLAB Programming 48 - plot a graph


To plot the graph of a function, you need to take the following steps:


Define x, by specifying the range of values for the variable x, for which the function is to be plotted

Define the function, y = f(x)

Call the plot command, as plot(x, y)

Following example would demonstrate the concept.

Let us plot the simple function y = x for the range of values for x from 0 to 100, with an increment of 5.


Create a script file and type the following code:


x = [0:5:100];
y = x;
plot(x, y)

When you run the file, MATLAB displays the following plot:







Wednesday, 3 August 2016

MATLAB Programming 47 - comparison two strings


Comparing Strings


Create a script file and type the following code into it:


str1 = 'This is test'
str2 = 'This is text'
if (strcmp(str1, str2))
sprintf('%s and %s are equal', str1, str2)
else
sprintf('%s and %s are not equal', str1, str2)
end

When you run the file, it displays the following result:


str1 =
This is test
str2 =
This is text
ans =
This is test and This is text are not equal