Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Some help on textscan

Subject: Some help on textscan

From: Mukesh

Date: 6 Aug, 2012 15:11:10

Message: 1 of 5

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);

I've few questions:
1) In 5th line MATLAB suggests me to use TEXTSCAN instead of STRREAD.

so in place of 5th line I use these two lines.

data_headers = textscan(data_header,'%s','delimiter',',');
numvars = numel(data_headers{:});

How can I eliminate the 2nd line. I tried..

data_headers = numel(textscan(data_header,'%s','delimiter',','){:});

didn't work..

2) I want to use TEXTSCAN instead of FGETL to get the header line, and modify the further code to get the variables in ws.

3) I want to avoid EVAL, any alternate?

Thankyou.

Subject: Some help on textscan

From: dpb

Date: 6 Aug, 2012 17:38:35

Message: 2 of 5

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);

--

Subject: Some help on textscan

From: dpb

Date: 6 Aug, 2012 19:28:28

Message: 3 of 5

On 8/6/2012 12:38 PM, dpb wrote:
...

> 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);

Ooops, the above will return a column vector as written so you'd need
the transpose _after_ the reshape()...

data=reshape(data,nVar,[])';

Or, since do have nVar at the point, your use of repmat in the format
will work but you'll also want " 'collectoutput', 1 " to group the data

--

Subject: Some help on textscan

From: Mukesh

Date: 7 Aug, 2012 16:12:07

Message: 4 of 5

dpb <none@non.net> wrote in message <jvp5ss$uq1$1@speranza.aioe.org>...
> On 8/6/2012 12:38 PM, dpb wrote:
> ...
>
> > 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);
>
> Ooops, the above will return a column vector as written so you'd need
> the transpose _after_ the reshape()...
>
> data=reshape(data,nVar,[])';
>
> Or, since do have nVar at the point, your use of repmat in the format
> will work but you'll also want " 'collectoutput', 1 " to group the data
>
> --

Thankyou very much, use of STRFIND is excellent here.

As suggested by you..

data=reshape(data,nVar,[])';

Is not the output of TEXTSCAN a 1x1 cell, so we should use ...

data=reshape(data{:},nVar,[])';

or maybe you just missed it while typing? I'm on R2012a but that shouldn't make a difference I guess.

Any idea on skipping the EVAL for importing the data with headers as variable name as shown by my original code.

Thankyou

Subject: Some help on textscan

From: dpb

Date: 7 Aug, 2012 17:00:28

Message: 5 of 5

On 8/7/2012 11:12 AM, Mukesh wrote:
...

> data=reshape(data,nVar,[])';
>
> Is not the output of TEXTSCAN a 1x1 cell, so we should use ...
>
> data=reshape(data{:},nVar,[])';
>
> or maybe you just missed it while typing? I'm on R2012a but that
> shouldn't make a difference I guess.

Yes. My release predates textscan() and so I'm used to textread() which
returns arrays, not cells. In many instances that's much more
convenient altho there's advantages in irregular data w/ cells. Ideally
there should be the choice...

> Any idea on skipping the EVAL for importing the data with headers as
> variable name as shown by my original code.

Yes...just don't do that.

Use dynamic field names instead.

See the FAQ

<http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F>

and

<http://matlab.wikia.com/wiki/FAQ#Why_is_it_advised_to_avoid_using_the_.22eval.22_function.3F>

--

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us