Read data in Matlab
조회 수: 22 (최근 30일)
이전 댓글 표시
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
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
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
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:
- 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:
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Import and Export에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!