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
Star Strider 2014 年 6 月 3 日

1 投票

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
Siddharth Pande 2014 年 6 月 3 日
no it didnt work the error is as follows: ??? Undefined function or method 'readtable' for input arguments of type 'char'.
Error in ==> rfile at 14 T = readtable('filename', 'HeaderLines', 5, 'Delimiter','\t','ReadVariableNames',false);
Star Strider
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
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
Siddharth Pande 2014 年 6 月 3 日
sorry but i still have the same error i want to know whether 'readtable' is your custom function,as it does not shows up in the help menu I m using 2010b version of matlab. the file runs perfectly till the Q after that it shows error.
clc;
clear all;
close all;
[filepath filename] = uigetfile('*.cir'); % selecting a circuit file
MYFILE = strcat(filename,filepath); % connecting filename with path
cmd_line='C:\spice\bin\ngspice.exe '; % to call external function
run_line = [cmd_line, MYFILE]; %to run in system
Q = system(run_line);
%
%
%---------------------------ERROR-------------------------------
%call the output generated from the external program in .dat form
[filepath filename] = uigetfile('*.dat');
DATA = strcat(filename,filepath);
MYDATA=fopen('filename');
disp(filename);
disp(filepath);
T = readtable(DATA, 'HeaderLines', 5, 'Delimiter','\t','ReadVariableNames',false);
Time = T.Var2;
iv1 = T.Var3;
figure(1)
plot(Time, iv1)
grid
%------------------------------------------------------------------
Star Strider
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
Siddharth Pande 2014 年 6 月 4 日
thanks a lot for your help i had to make a little modification ,it worked thanks again
Star Strider
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
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
Siddharth Pande 2014 年 11 月 17 日
download my txt file now take star strider's code to the command line of matlab and see how it works try to use matlab 2013. as text scan function is provided in latest version
if you get any idea from it then you could simply chang yours
be sure when working with your file change the .dat file to .txt and run the code search for cell2mat function and textscan function in matlab help
Star Strider
Star Strider 2014 年 11 月 17 日
@Siddharth Pande —
I very much appreciate your recommending my Answer. Now would your please Accept it?

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

カテゴリ

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

質問済み:

2014 年 6 月 3 日

コメント済み:

2014 年 11 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by