|
On 8/6/2012 10:11 AM, Mukesh wrote:
> I am trying to import a text data file with comma as data delimiter.
> This is how the file looks like.
>
> test data created on dd/mm/yy
>
> aa,bb,cc,dd,ee,ff
>
> 0.19,0.42,0.16,0.25,0.98,0.32
> 0.31,0.76,0.73,0.95,0.28,0.87
> 0.42,0.16,0.22,0.87,0.19,0.08
> 0.08,0.30,0.78,0.14,0.56,0.62
> 0.07,0.60,0.22,0.77,0.08,0.91
> 1.00,0.15,0.00,0.17,0.34,0.17
> 0.33,0.90,0.35,0.74,0.86,0.15
> 0.34,0.34,0.43,0.66,0.70,0.15
> 0.63,0.22,0.51,0.54,0.04,0.60
> 0.36,0.20,0.60,0.65,0.50,0.04
>
> this is the my code for importing this data.
>
> fid = fopen('data.txt');
> fgetl(fid); % skip the 1st test data line
> fgetl(fid); % skip the 2nd blank line
> data_header = fgetl(fid); % get the header line aa,bb,cc,dd,ee,ff
> numvars = numel(strread(data_header,'%s','delimiter',','));
> fgetl(fid); % skip the 4th blank line
> data = textscan(fid,repmat('%f',1,numvars),'delimiter',',');
> eval(['[' data_header '] = deal(data{:});'])
> fclose(fid);
>
...
The "Matlab way" would be to use the array column indices as surrogates
for the individual variable names. Or use structure w/ the variable as
the name.
The simpler way would be to simply
fid = fopen('data.txt');
data = textscan(fid,'%f','delimiter',',','headerlines',4);
If the number of variables in a record is variable, then the simple way
to find out from the header line would be sotoo what you presently have
to read the header info instead of skipping the header lines...
fid = fopen('data.txt');
l=fgetl(fid);
l=fgetl(fid);
l=fgetl(fid);
nVar=length(strfind(l,','))+1;
data = textscan(fid,'%f','delimiter',',','headerlines',1);
reshape(data',nVar,[]);
fid=fclose(fid);
--
|