user3306583
user3306583

Reputation: 119

Plotting from .dat file in Octave

I am running a Fortran code that is outputting several sets of data to one .dat file. I want to plot the data, which is in two columns, using Octave. For example, the data I want to plot is in the following form:

      t              Eta(t)
     0.00           -0.748
     0.50           -0.773
     1.00           -0.774
     1.50           -0.535
     2.00           -0.120
     2.50            0.131
     3.00            0.184
     3.50            0.211
     4.00            0.068
     4.50           -0.110

Where the data starts on line 148 (not including the t and eta(t) line) and continues until line 247. The data for t and eta(t) obviously span across a few columns (according to the text file, that's where I'm reading the lines and column numbers from). Is there any way to plot the data if it's in this form by specifying which line to start from or anything? Put simply, I want to plot it so that the values in the t column are on the x axis and the eta(t) values are on the y axis. Thanks in advance for any help!

Upvotes: 0

Views: 8268

Answers (2)

ederag
ederag

Reputation: 2509

A much faster alternative to textread (see Andy's answer) is dlmread.

filename = "yourfile.dat";
# empty separator means 'automatic'
separator = '';
skipped_rows = 147;
skipped_columns = 0;
m = dlmread(filename, separator, skipped_rows, skipped_columns);
t = m(:,1);
eta_ = m(:, 2);
plot(t, eta_)

If you want to stop reading the file after line 248, use

filename = "yourfile.dat";
# empty separator means 'automatic'
separator = '';
skipped_rows = 147;
skipped_columns = 0;
last_row = 248
last_column = 2
m = dlmread(filename, separator, 
            [skipped_rows, skipped_columns, last_row-1, last_column-1]);
t = m(:,1);
eta_ = m(:, 2);
plot(t, eta_)

Upvotes: 2

Andy
Andy

Reputation: 8091

Use textread. You want to skip 147 lines (headerlines) and read 247-147 = 100 lines

[t,eta] = textread ("yourfile.dat", "%f %f", 100, "headerlines", 147)

This will return 2 columns vectors "t" and "eta" with your data. After this you can plot them with

plot (t, eta)

Is you .dat online available or could you upload it?

Upvotes: 1

Related Questions