dlmread reading file incorrectly

8 views (last 30 days)
Hello,
i wrote a code using the following lines:
clc
clear all
load('radius.mat')
fidEK = fopen('PerEk.dat','r');
filename = 'PerEk.dat';
PerEK = dlmread(filename,' ',2,0); %reading the PerEK.dat file
the file that i read is for example:
VARIABLES = "X","Y","PerEk"
ZONE I= 501, J= 501 DATAPACKING=POINT
0.99800E-03 0.99800E-03 0.61593E-11
0.99800E-03 0.29940E-02 0.98748E-10
0.99800E-03 0.49900E-02 0.45617E-09
0.99800E-03 0.69860E-02 0.11994E-08
what i used to receive was as following:
0.99800E-03 0.99800E-03 0.61593E-11
0.99800E-03 0.29940E-02 0.98748E-10
0.99800E-03 0.49900E-02 0.45617E-09
0.99800E-03 0.69860E-02 0.11994E-08
and now for some reason i'm getting one column. can anyone help? i tried changing
PerEK = dlmread(filename,' ',2,0);
into
PerEK = dlmread(filename,'\t',2,0);
didn't work.

Accepted Answer

Stephen23
Stephen23 on 9 Mar 2016
Edited: Stephen23 on 9 Mar 2016
The data you have shown does not use tab as the delimiter, but uses four space characters.
You could use textscan, which has an option to merge repeated delimiters:
opts = {'HeaderLines',2, 'MultipleDelimsAsOne',true};
fid = fopen('temp.txt','rt');
M = cell2mat(textscan(fid,'%f%f%f',opts{:}));
fclose(fid);
imports this:
>> M
M =
9.9800e-004 9.9800e-004 6.1593e-012
9.9800e-004 2.9940e-003 9.8748e-011
9.9800e-004 4.9900e-003 4.5617e-010
9.9800e-004 6.9860e-003 1.1994e-009
  1 Comment
Shahar Idan
Shahar Idan on 9 Mar 2016
thank you so much! saved my hours for my research! i noticed that the function dlmread was changed between matlab 2014 and 2016 an apparently my university updated to the newest version causing the function to read incorrectly '\t' or TAB. again, thanks.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!