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:
Read tab delimited files with header file

Subject: Read tab delimited files with header file

From: edward kabanyas

Date: 28 May, 2012 02:05:07

Message: 1 of 4

Dear all;

I have two types of tab delimited files, first separated with ' ' or 'space' and ';' or 'semi'. All files are in CSV type and have a header file which is not variable names separated by the tabs.

For the first type (separated with space), I read as:

fid = fopen(fname1);
fmt=['%s %s %f %f %d %s %s %f %d %d %d %d %f %f %s' repmat('%8.1f',1,1024) '%s'];
data = textscan(fid,fmt,'HeaderLines',1,'Delimiter',',','CollectOutput',1);

However, for the second type (separated with ';') I have problem to read it.
I tried to use tdfread, but the header file is not variable names separated by the tabs and the number of header column is not the same as the second row (data). In another data, data separated with ';' are without header file.

Probably you have expecrience in reading this kind of data, separated with ';' but without variable names in the first row.

Thank you,

Edward

Subject: Read tab delimited files with header file

From: TideMan

Date: 28 May, 2012 02:45:56

Message: 2 of 4

On Monday, May 28, 2012 2:05:07 PM UTC+12, edward kabanyas wrote:
> Dear all;
>
> I have two types of tab delimited files, first separated with ' ' or 'space' and ';' or 'semi'. All files are in CSV type and have a header file which is not variable names separated by the tabs.
>
> For the first type (separated with space), I read as:
>
> fid = fopen(fname1);
> fmt=['%s %s %f %f %d %s %s %f %d %d %d %d %f %f %s' repmat('%8.1f',1,1024) '%s'];
> data = textscan(fid,fmt,'HeaderLines',1,'Delimiter',',','CollectOutput',1);
>
> However, for the second type (separated with ';') I have problem to read it.
> I tried to use tdfread, but the header file is not variable names separated by the tabs and the number of header column is not the same as the second row (data). In another data, data separated with ';' are without header file.
>
> Probably you have expecrience in reading this kind of data, separated with ';' but without variable names in the first row.
>
> Thank you,
>
> Edward

Just replace comma with semicolon in delimiter and remove headerlines:
data = textscan(fid,fmt,'Delimiter',';','CollectOutput',1);

Subject: Read tab delimited files with header file

From: edward kabanyas

Date: 28 May, 2012 02:51:06

Message: 3 of 4

For the second type (separated with ';'), I try as follow:


fmt=repmat(';',1,1039);
data = tdfread(fname1,fmt);

However, because the header file is not variable names separated by the tabs, the data structure is not correct. Thanks for help,

Edward


"edward kabanyas" <djuky_hmi@yahoo.com> wrote in message <jpumgj$rru$1@newscl01ah.mathworks.com>...
> Dear all;
>
> I have two types of tab delimited files, first separated with ' ' or 'space' and ';' or 'semi'. All files are in CSV type and have a header file which is not variable names separated by the tabs.
>
> For the first type (separated with space), I read as:
>
> fid = fopen(fname1);
> fmt=['%s %s %f %f %d %s %s %f %d %d %d %d %f %f %s' repmat('%8.1f',1,1024) '%s'];
> data = textscan(fid,fmt,'HeaderLines',1,'Delimiter',',','CollectOutput',1);
>
> However, for the second type (separated with ';') I have problem to read it.
> I tried to use tdfread, but the header file is not variable names separated by the tabs and the number of header column is not the same as the second row (data). In another data, data separated with ';' are without header file.
>
> Probably you have expecrience in reading this kind of data, separated with ';' but without variable names in the first row.
>
> Thank you,
>
> Edward

Subject: Read tab delimited files with header file

From: edward kabanyas

Date: 28 May, 2012 03:03:06

Message: 4 of 4

TideMan,

Thanks for your reply.

Your solution is very nice, it works. However, suppose I have many lines data in a file as below:

28.05.2011;00:00:00;2.161;20.60;58;+RADZ;RL+;24.645;5156;11064;178;14;0.06;24.....and soon

28.05.2011;00:00:10;2.349;20.60;58;+RADZ;RL+;25.392;4643;11057;187;15;0.06;24...and soon

the first and the second line is separated with a space (empty line), probably you have other suggestion to read it. Thanks again for help,

Edward


 <mulgor@gmail.com> wrote in message <941e5419-42fe-41c6-8b3c-7029119ac93a@googlegroups.com>...
> On Monday, May 28, 2012 2:05:07 PM UTC+12, edward kabanyas wrote:
> > Dear all;
> >
> > I have two types of tab delimited files, first separated with ' ' or 'space' and ';' or 'semi'. All files are in CSV type and have a header file which is not variable names separated by the tabs.
> >
> > For the first type (separated with space), I read as:
> >
> > fid = fopen(fname1);
> > fmt=['%s %s %f %f %d %s %s %f %d %d %d %d %f %f %s' repmat('%8.1f',1,1024) '%s'];
> > data = textscan(fid,fmt,'HeaderLines',1,'Delimiter',',','CollectOutput',1);
> >
> > However, for the second type (separated with ';') I have problem to read it.
> > I tried to use tdfread, but the header file is not variable names separated by the tabs and the number of header column is not the same as the second row (data). In another data, data separated with ';' are without header file.
> >
> > Probably you have expecrience in reading this kind of data, separated with ';' but without variable names in the first row.
> >
> > Thank you,
> >
> > Edward
>
> Just replace comma with semicolon in delimiter and remove headerlines:
> data = textscan(fid,fmt,'Delimiter',';','CollectOutput',1);

Tags for 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