how to use the data from .dat file for plotting graph
이전 댓글 표시
I generated a data file in .dat form using a spice tool now I want to call this data file in matlab and use the x and y coordinates from the data file to plot the data in matlab .I am attaching the file having my data it is in txt format now as the attachments doesn't accept .dat format so anyone working with my file plz change the extension to .dat.
답변 (2개)
Star Strider
2014년 6월 3일
I do not know if this would work for ‘.dat’ files, but it works for the ‘.txt’ file you posted:
T = readtable('switch.txt', 'HeaderLines', 5, 'Delimiter','\t','ReadVariableNames',false);
Time = T.Var2;
iv1 = T.Var3;
figure(1)
plot(Time, iv1)
grid
댓글 수: 7
Siddharth Pande
2014년 6월 3일
Star Strider
2014년 6월 3일
편집: Star Strider
2014년 6월 3일
It worked perfectly for the text file. Just rename your ‘.dat’ files as ‘.txt’ files and use them that way.
First, if you defined:
filename = 'switch.txt';
the readtable line should be:
T = readtable(filename, 'HeaderLines', 5, 'Delimiter','\t','ReadVariableNames',false);
There should not be any quotes around filename in your call to readtable.
You didn’t post all your relevant code for opening the file and reading it, so I have no way of knowing what else you did, figuring out what’s wrong, and perhaps fixing it. The point is that reading it as a ‘.txt’ file works.

Kelly Kearney
2014년 6월 3일
The readtable function wasn't introduced until very recently (R2013b, I think). Anyone running an older version (and I would assume that's a lot of people, including myself) could try dataset as an alternative (if they have the Statistics Toolbox), or textscan if not.
Siddharth Pande
2014년 6월 3일
Star Strider
2014년 6월 3일
I haven’t used textscan since readtable appeared. This should work:
filename = 'switch.txt';
dfin = fopen(filename, 'r'); % Open data file for reading
datin = textscan(dfin, '%f %f %f', 'HeaderLines',5); % Read data, skip 5 header lines
Time = cell2mat(datin(2)); % Define variables
iv1 = cell2mat(datin(3));
The plot call doesn’t change, nor does the plot.
Siddharth Pande
2014년 6월 4일
Star Strider
2014년 6월 4일
편집: Star Strider
2014년 6월 4일
My pleasure!
(The sincerest form of appreciation here on MATLAB Answers is to Accept the Answer that most closely solves your problem.)
Daniel
2014년 10월 17일
0 개 추천
Hi guys, I have a similar problem. I saved some data in a .dat file. I can load the data to matlab and it saves it in an array but I don't know how to plot a graph using the data. Any help would be appreciated.
댓글 수: 2
Siddharth Pande
2014년 11월 17일
Star Strider
2014년 11월 17일
@Siddharth Pande —
I very much appreciate your recommending my Answer. Now would your please Accept it?
카테고리
도움말 센터 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!