MATLAB Answers

0

How to make a 2D graph from text file

Asked by Dennis Wikan on 4 Apr 2017
Latest activity Edited by Joseph Cheng
on 5 Apr 2017
I want the x-axis to represent the time and the y-axis to represent pressure. This is what I am working with now: fid=fopen('1.sec.txt','rt') for i=1:2, fgetl(fid),end M = dlmread('1.sec.txt',';',4,0) A=fscanf(fid,'%f',[2,inf])'; fclose(fid) subplot(2,1,1) plot(A(:,1),A(:,2))
And this is the error message the program gives me: Index exceeds matrix dimensions.
Error in txtfiltest (line 7) plot(A(:,1),A(:,2))

  0 Comments

Sign in to comment.

1 Answer

Answer by Joseph Cheng
on 5 Apr 2017
Edited by Joseph Cheng
on 5 Apr 2017

I can point out a few things.
  1. if you look at M isn't that what you're trying to plot with A? as it should include both columns of data.
  2. your fscanf starts off at with a row of t;p which it doesn't recognize.
fid=fopen('test.txt','rt')
for i=1:4, fgetl(fid),end
M = dlmread('test.txt',';',4,0)
A=fscanf(fid,'%f;%f',[2,inf])';
fclose(fid)
subplot(2,1,1),plot(A(:,1),A(:,2))
the above code is rigged with a test case i ran of a similar file like your picture. so instead of just 2 fgetl's i do 4 to get the position in the file to the starting point of the data. Also I put the ';%f' in there as i do not think it recognizes ';' as a deliminator. I cannot find the documentation but from the examples of fscanf i think it is looking for a \tab. Hopefully someone comes along and corrects me on that

  0 Comments

Sign in to comment.