create a graph in matlab

Creating a simple 2D plot in Matlab is straightforward using the plot function. Firstly, define the x and y data you wish to plot. Then, call the plot function with these data arrays as input arguments.

main.m
x = 0:0.01:2*pi;
y = sin(x);
plot(x,y);
40 chars
4 lines

This example will plot the sin of the variable x versus x. You can customize the plot by adding axis labels, legends, and adjusting the line style or color.

For example, setting the line style to a dashed line, adding axis labels and a title, and changing the line color to red:

main.m
x = 0:0.01:2*pi;
y = sin(x);
plot(x,y,'--r');
xlabel('x');
ylabel('sin(x)');
title('Plot of sin(x)');
102 chars
7 lines

This will create a plot with a red dashed line, labeled x and sin(x) along the respective axis, and with the title "Plot of sin(x)".

You can also create different types of plots such as scatter plots, bar graphs, and histograms, using various built-in plotting functions available in Matlab.

gistlibby LogSnag