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:
how to read/import multiple files which have headings

Subject: how to read/import multiple files which have headings

From: julia

Date: 9 Jun, 2008 13:36:01

Message: 1 of 8

Hi,
I am a beginner when it comes to Matlab and am trying now
for some time to get my data in the most efficient way into
matlab for further processing. Its rather a lot of files
(Test1, Test2 ....) which have some headers and are
delimited with ; in ASCI format. I was hoping that there is
some automation possible:

My input gives me an error with the textread and i dont
really know how to fix it. Any help appreciated !
-----------
clear all; close all;
for k = 1:50
  textfilename = ['Test' num2str(k) '.ASC'];
  fid = fopen(textfilename, 'rt');
  data = textread(fid,'','delimiter',';','headerlines',7);
  savefile=['Test' num2str(k) '.mat'];
  nrows=size(data,1);
  ncolumns=size(data,2);
  data=data(:,1:ncolumns-1);
  lengte = length(data);
  Time=data(1:lengte,1);
% then come lots of variable & calc etc which are fine
which I omitted here...
  save(savefile,'Time');
  fclose(fid);
end
--------

Subject: how to read/import multiple files which have headings

From: French Caro

Date: 9 Jun, 2008 13:49:02

Message: 2 of 8

"julia " <julia.krohn@gmail.com> wrote in message
<g2jbk1$ht2$1@fred.mathworks.com>...
> Hi,
> I am a beginner when it comes to Matlab and am trying now
> for some time to get my data in the most efficient way into
> matlab for further processing. Its rather a lot of files
> (Test1, Test2 ....) which have some headers and are
> delimited with ; in ASCI format. I was hoping that there is
> some automation possible:
>
> My input gives me an error with the textread and i dont
> really know how to fix it. Any help appreciated !
> -----------
> clear all; close all;
> for k = 1:50
> textfilename = ['Test' num2str(k) '.ASC'];
> fid = fopen(textfilename, 'rt');
> data = textread(fid,'','delimiter',';','headerlines',7);
> savefile=['Test' num2str(k) '.mat'];
> nrows=size(data,1);
> ncolumns=size(data,2);
> data=data(:,1:ncolumns-1);
> lengte = length(data);
> Time=data(1:lengte,1);
> % then come lots of variable & calc etc which are fine
> which I omitted here...
> save(savefile,'Time');
> fclose(fid);
> end
> --------
>
>
>
Hi,
I think that you should use texscan in place of textread.
Both do the same thing but don't have the same inputs :
textread take as first parameter the name of the file not
the fid while textscan take the fid (file identifier) as
first input.

Caroline

Subject: how to read/import multiple files which have headings

From: Andres

Date: 9 Jun, 2008 19:24:01

Message: 3 of 8

"julia " <julia.krohn@gmail.com> wrote in message
<g2jbk1$ht2$1@fred.mathworks.com>...
> Hi,
> I am a beginner when it comes to Matlab and am trying now
> for some time to get my data in the most efficient way into
> matlab for further processing. Its rather a lot of files
> (Test1, Test2 ....) which have some headers and are
> delimited with ; in ASCI format. I was hoping that there is
> some automation possible:
[...]

Hi,
you may also give txt2mat from the file exchange a try.
According to your file format description, simply write

A = txt2mat;

to browse manually, or

A = txt2mat('c:\fileOrFolder');

which is probably a bit more convenient than using textscan
 (and similarly efficient).

Hth, regards,
Andres

Subject: how to read/import multiple files which have headings

From: julia

Date: 12 Jun, 2008 15:04:02

Message: 4 of 8


Great, in principle it works with textscan, except that
matlab seems to crash at file no 45 and i have plenty
more... any idea how to get around this ? The file is not
corrupted or anything as it is processed if I only then go
from 45 to the next 89 its ok again ?

Any help appreciated



"French Caro " <caro95470@nospam.free.fr> wrote in message
<g2jcce$qve$1@fred.mathworks.com>...
> "julia " <julia.krohn@gmail.com> wrote in message
> <g2jbk1$ht2$1@fred.mathworks.com>...
> > Hi,
> > I am a beginner when it comes to Matlab and am trying
now
> > for some time to get my data in the most efficient way
into
> > matlab for further processing. Its rather a lot of
files
> > (Test1, Test2 ....) which have some headers and are
> > delimited with ; in ASCI format. I was hoping that
there is
> > some automation possible:
> >
> > My input gives me an error with the textread and i dont
> > really know how to fix it. Any help appreciated !
> > -----------
> > clear all; close all;
> > for k = 1:50
> > textfilename = ['Test' num2str(k) '.ASC'];
> > fid = fopen(textfilename, 'rt');
> > data = textread
(fid,'','delimiter',';','headerlines',7);
> > savefile=['Test' num2str(k) '.mat'];
> > nrows=size(data,1);
> > ncolumns=size(data,2);
> > data=data(:,1:ncolumns-1);
> > lengte = length(data);
> > Time=data(1:lengte,1);
> > % then come lots of variable & calc etc which are fine
> > which I omitted here...
> > save(savefile,'Time');
> > fclose(fid);
> > end
> > --------
> >
> >
> >
> Hi,
> I think that you should use texscan in place of textread.
> Both do the same thing but don't have the same inputs :
> textread take as first parameter the name of the file not
> the fid while textscan take the fid (file identifier) as
> first input.
>
> Caroline

Subject: how to read/import multiple files which have headings

From: French Caro

Date: 12 Jun, 2008 15:24:02

Message: 5 of 8

"julia " <julia.krohn@gmail.com> wrote in message
<g2rdt2$gue$1@fred.mathworks.com>...
>
> Great, in principle it works with textscan, except that
> matlab seems to crash at file no 45 and i have plenty
> more... any idea how to get around this ? The file is not
> corrupted or anything as it is processed if I only then go
> from 45 to the next 89 its ok again ?
>
> Any help appreciated
>
Hi,

When you say that MATLAB crashes : what does it really do ?
Do you have any error message or crash dump ? or just nothing ?
And do you reach to read this file no 45 ?

Have you given Andres' proposal a try ?

Caroline

Subject: how to read/import multiple files which have headings

From: William

Date: 12 Jun, 2008 19:29:03

Message: 6 of 8

Another possibility, albeit a round about way, would be to
have excel convert those files then do xlsread to bring in
the Raw data in a cell towhich you can alter it as you see
fit. You could further automate this process if your
filenames follow the same format as you've posted (Test1,
Test2, etc) by using strcat and sprintf to run through all
the filenames (so long as the route directery stays the
same). Just some thought.

Subject: how to read/import multiple files which have headings

From: Marcus M. Edvall

Date: 12 Jun, 2008 19:52:37

Message: 7 of 8

Using xlsread might be very slow for you. Might want to run some tests
on this first before converting your code.

Best wishes, Marcus
Tomlab Optimization Inc.
http://tomdyn.com/

Subject: how to read/import multiple files which have headings

From: S?rgio Magalh?es

Date: 12 Feb, 2009 11:14:01

Message: 8 of 8

And what about if I had completely different file names and want to read all files in order to perform some simulations. How could I do it? The files have the extension *.odf and it can be opened as text using "Open as Text" in the current directory tab.
Could anyone help me? I tried quite a few ways and with (for instance) dlmread I only can read one file and It is what I don't want.

My best regards,

S?rgio

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