You are here: Home Help/User Support Programs for FEA, Modeling, etc. MATLAB Example of Plotting Simple 2D Data

Example of Plotting Simple 2D Data

Assume you have a numerical model built that has 1 independent variable, and 1 or more dependent variables, for example, a one-dimensional heat transfer problem where T or Q are functions of x. How can you make a simple line plot of the results?

Matlab provides facilities for both reading external data files, and for creating these kinds of plots. Here's a sample data file, simply named 'data':

  0.00 0.00
  0.10 0.01
  0.20 0.04
  0.30 0.09
  0.40 0.16
  0.50 0.25
  0.60 0.36
  0.70 0.49
  0.80 0.64
  0.90 0.81
  1.00 1.00

It's an ordered sequence of points, with each row containing an x, y combination. All we want to do here is to turn this data into a simple line graph.

To bring this data file into Matlab, you can use the load command:

  load -ascii data

Now we have a 11x2 matrix of numbers in a variable named data. We want to strip out each column into a separate variable:

  x=data(:,1)'; % strip out x axis values
  y=data(:,2)'; % strip out y axis values

The ' operator is a transpose, and transforms the variable from a column of numbers into a row. It's optional, but I think it's easier to read a row of numbers than a column.

Now we have a consistent set of (x,y) data that's ready to be graphed:

  zlabel('y = x^2')
  title('Simple line plot')

The final Matlab program file is shown below:

  load -ascii data
  x=data(:,1)'; % strip out x axis values
  y=data(:,2)'; % strip out y axis values
  title('Simple line data plot')

The final output is shown below:

Matlab Screenshot: XY Plot

Filed under: