Plotting certain columns from a .dat file

Greetings all,
It's been quite some time since I've used MATLAB, and embarrasingly I've been struggling with this all day. Needless to say I am a bit rusty! I have a data file that has 8 columns of data (and has headers at the top). Right now I'm trying to just plot column 2 of the data on the x-axis and plot the 4th and 8th columns as two line plots along the y-axis.
I imagine you use fopen and textscan, but there's so many to choose from. Maybe importdata?
Any help would be appreciated. Thanks!

 採用された回答

Fangjun Jiang
Fangjun Jiang 2011 年 9 月 20 日

1 投票

If your data file format is consistent, try
data=importdata('MyFile.data');
For plotting, if you have a matrix A, A(:,2) is all the data in the second column so you can do:
plot(A(:,2),A(:,4),'r',A(:,2),A(:,8),'b');

6 件のコメント

Jesse
Jesse 2011 年 9 月 20 日
Fangjun, ok thanks, but how would you go about plotting certain columns of the file as stated in the above question?
Jesse
Jesse 2011 年 9 月 20 日
Fangjun, ok thanks, but how would you go about plotting certain columns of the file as stated in the above question?
Fangjun Jiang
Fangjun Jiang 2011 年 9 月 20 日
See update.
Jesse
Jesse 2011 年 9 月 20 日
Fangjun, thanks again. However, I did what you said:
data = importdata('MyFile.dat');
plot(data(:,2),data(:,4),'r',data(:,2),data(:,8),'b');
But I receive
??? Index exceeds matrix dimensions.
Error in ==> Tester2 at 5
plot(data(:,2),data(:,4),'r',data(:,2),data(:,8),'b');
data is somehow a 1x1 struct, not a matrix. Should I try another function?
Fangjun Jiang
Fangjun Jiang 2011 年 9 月 21 日
data is a structure, type data in Command Window to see what it contains. You need to do A=data.data.
You've used MATLAB long time ago so maybe going through the "Getting Started" in MATLAB document is beneficial.
Jesse
Jesse 2011 年 9 月 21 日
Fangjun,
Ah yes! I forgot about the . operator to make the matrices agree!
Thanks - that worked!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by