How to Read From Multiple Files in Matlab

Multiple plots

The plot command can plot several sets of vectors.

Plot the functions y1 = sin(two pi x) and y2 = cos(two pi x) for x in the interval [0, 1] using 401 every bit spaced points.

Create a vector x of 401 as spaced points on [0, 1].

Create a vector y1 of role values.

Create a vector y2 of function values.

Plot both sets points in the same effigy.

Add a grid.

Add a championship. What is nevertheless missing?

>>  x = linspace(0, 1, 401);

>>  y1 = sin(2*pi*x);

>>  y2 = cos(2*pi*x);

>>  plot(ten, y1, x, y2)

>>  grid on

>>  title('sin(2 \pi x) and cos(two \pi x)')

sin(2 pi x) and cos(2 pi x)

In general y'all can use

plot(x1, y1, s1, x2, y2, s2, x3, y3, s3)

where x1 and y1 are vectors of the same length and s1 is an optional cord.

Legends

When there are multiple plots in the same figure it is a practiced thought to add together a fable, using, for example,

legend(string1, string2, string3)

Here string1 is a string describing the first set of values plotted, string2 is a string describing the 2d set of values plotted, and string3 is a string describing the third ready of values plotted.

You can use the mouse to reposition the legend box inside the plot, or you can specify the location of the legend box. See

help fable

for more information.

Add a legend to the previous plot.

The offset cord describes the outset information set, the second cord the second information ready.

>>  legend('sin(two \pi 10)', 'cos(two \pi x)')

Plot with legend

The hold command

If you lot have already created a plot and later wish to add another plot, then the hold command is useful.

Create the previous plot using 2 divide plot commands and the agree command.

Create the start plot assuming x, y1 and y2 are defined every bit above.

Turn the agree on.

Add the second plot.

Plough the hold off.

>>  plot(ten, y1)

>>  hold on

>>  plot(ten, y2)

>>  hold off

Plot using hold

Note that when using a single plot command, MATLAB adjusts the colours for successive plots. When using the hold command you must explicitly prepare the colours, for example using plot(10, y2, 'yard').

Subplots

Sometimes you desire a unmarried figure containing several individual subplots. The MATLAB command

subplot(chiliad, n, k)

creates an m by due north assortment of plots and positions you lot at plot number k, where the plots are numbered counting beyond rows.

The most common examples are

  • a 2 by i filigree of subplots for two plots one on top of each other;
  • a 1 past ii filigree for two plots side by side.

Hither is a 2 by 2 grid of subplots to make it articulate how the numbering of subplots works.

Create plots of sin(yard pi x) for ten on [0, 1] and m = ane, ii, three, 4 in one effigy.

Create vector of plot points.

2 past two filigree of subplots, at subplot 1.

Add plot on electric current subplot.

Add together grid and title on current subplot.

2 by 2 grid of subplots, at subplot ii.

Add plot on current subplot.

Add grid and title on electric current subplot.

ii by 2 filigree of subplots, at subplot 3.

Add plot on current subplot.

Add grid and title on current subplot.

2 past 2 grid of subplots, at subplot iv.

Add plot on electric current subplot.

Add grid and title on current subplot.

>>  10 = linspace(0, 1, 401);

>>  subplot(two,2,one)

>>  plot(ten, sin(one*pi*x))

>>  grid on; title('k = i')

>>  subplot(ii,2,2)

>>  plot(ten, sin(2*pi*ten))

>>  grid on; championship('k = 2')

>>  subplot(two,2,3)

>>  plot(ten, sin(3*pi*x))

>>  grid on; title('grand = 3')

>>  subplot(two,two,4)

>>  plot(x, sin(four*pi*x))

>>  grid on; title('thousand = 4')

Four subplots

Axis limits

If you lot are really observant y'all volition accept noticed that the limits of the y-centrality on the offset subplot is from 0 to one, while the other 3 plots all have y ranging from -ane to i. MATLAB tries to choose adept axis limits based on the data that is beingness plotted. Nevertheless sometimes you want to change the axis limits.

The simplest way to do this is to apply

xlim([xmin xmax])

to make the x-axis run from xmin to xmax. To adjust the limits of the y-axis use

ylim([ymin ymax])

to make the y-axis run from ymin to ymax. Note that the argument to xlim and ylim is a 2 element vector giving the lower and upper limits for the axis.

Another way to control the limits and scaling of the axes is to use the axis command, for example

axis([xmin xmax ymin ymax])

By default the 10-scale is slightly larger than the y-scale, so you get a rectangular plot on the screen. To make the axis scaling equal utilise

centrality equal

which is required to make a circle look like a circle!

Equally e'er, more than information can be obtained by typing

help axis

in the MATLAB control window.

Plot the two circles (cos(t), sin(t)) and (cos(t), sin(t))/2 for t in [0, two pi]. Ensure the plot looks like circles non ellipses! Make both the x-axis and y-axis both become form -ane.1 to 1.1.

Create vector t of parameter values.

Store the values of cos(t) and sin(t).

Plot the two curves.

Make the axis scaling equal.

Adjust the axis limits.

Add together a grid.

>>  t = linspace(0, 2*pi, 401);

>>  ct = cos(t); st = sin(t);

>>  plot(ct, st, ct/2, st/2)

>>  centrality equal

>>  centrality([-1.1 1.i -1.one ane.1])

>>  grid on

Plotting circles

Figures

MATLAB draws a plot in the current figure window. Figure windows are labelled i, 2, 3, ... The command

figure

creates a new figure window. Alternatively the command

figure(k)

where k is a positive integer, opens figure window k if it already exists, or creates figure window k if it does not exist.

Summary

The plot command tin can plot several sets of data on the one set up of axes. In this instance a legend should be added.

Several different plots inside the 1 figure can be created using the subplot control.

Axis limits and scaling can exist modified with the xlim, ylim and centrality commands.

morabutedinefs.blogspot.com

Source: https://www.maths.unsw.edu.au/sites/default/files/MatlabSelfPaced/lesson5/MatlabLesson5_MultiplePlots.html

Related Posts

0 Response to "How to Read From Multiple Files in Matlab"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel