file .dat

file .dat

Post by Isabell » Wed, 18 Sep 2002 21:13:47



how can i read a file .dat with matlab?
Thanks
Isabella
 
 
 

file .dat

Post by Norbert Marwa » Wed, 18 Sep 2002 21:36:39


Firstly, it depends on the structure of the file. I think, it is an
ASCII file.
You can use various commands. Please look at the Matlab help for the
commands above.

1) d=load('file.dat');
???if there are only numbers in the file.

2) x=dlmread('file.dat',[1 0 20 3]);
???if you will read only the first 4 columns
???and the rows until rows 21 without the
???first row.

3) [z1 z2]=textread('file.dat','%s %s');
???if there are two columns. letters are
???allowed in the file.

4) the most generalized possibility you
???have with fopen/fgetl commands, which
???allows you to read formatted files.
?
fid=fopen('zurich');
i=0;
clear x
while 1
??i=i+1;
??s=fgetl(fid);
??if ~ischar(s), break, end
??x(i,1)=str2num(s(1:4));
??x(i,2:13)=str2num(reshape(s(5:end),7,12)')';

end
fclose(fid)

In (1)-(3) the data in the file must be delimitered, whereas in (4)
the data must be presented in a fixed structure. All above are
examples for special numbers of columns/ rows and formatted datas,
resp., which you have to modify to your data structure. However, with
this commands you will find enough help in Matlab.

Norbert


> how can i read a file .dat with matlab?
> Thanks
> Isabella