Read data in Matlab

14 次查看(过去 30 天)
Yro
Yro 2019-8-29
Hi, how can I read and plot data from a file in Matlab. I have the following data format:
# Axial
0.26889788E+11
0.89731269E+11
0.15931918E+15
0.20239994E+15
0.24342588E+15
0.28013145E+15
...
I have used the textscan function but I have not been able to obtain results. I tried something like this:
fileID = fopen ('/path/to/textfile', 'r+');
data = textscan (fileID, %f%f);
celldisp(data)
but I get an empty array
data = [ ]
Regards, thanks in advance.
  2 个评论
Rik
Rik 2019-8-29
To avoid confusion, did you use this code, or did you actually use data=textscan(fileID,'%f%f');?
As for you question, you need to skip the first line in your processing. It would also help if you provided a sample file so it is possible to write code that works for your data.
dpb
dpb 2019-8-29
The headerline would cause failure with the format string either way...
data = cell2mat(textscan(fileID, '%f%f',headerlines',1);
You might look at importdata(); it will handle simple file structures such as this automagically for you.

请先登录,再进行评论。

回答(1 个)

Kritika Bansal
Kritika Bansal 2019-9-9
Hi,
Assuming that you are using MATLAB R2006a or above, you have the following options to read a text file in matlab:
  1. Using textscan()
fid = fopen('data.txt', 'rt'); %opens the file
data = textscan(fid, '%f%f', 'HeaderLines', 1); %skips the first line
ar = data{1}; %retrieves the column vector
2. Using readmatrix()
data = readmatrix('data.txt', 'NumHeaderLines', 1); %reads the data in column vector
3. Using importdata()
y= importdata('data.txt','',1); %reads the data as a structure into 3 fields
ar = y.data; %retrieves the column vector
You can find the documentation link of these functions below:

Community Treasure Hunt

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

Start Hunting!

Translated by