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:
importdata problem !!

Subject: importdata problem !!

From: Egli Vorfi

Date: 17 Nov, 2010 12:17:04

Message: 1 of 8

Hello,

I'm trying to load a .txt file in matlab for statistical manipulations (mean, plotting, etc).

When I use importdata() it creates a cell variable, and I need a structure... The problem is, that I don't have this problem all time.

For example :

If a use this text file :

Day1 Day2 Day3 Day4 Day5 Day6 Day7
95.01 76.21 61.54 40.57 5.79 20.28 1.53
23.11 45.65 79.19 93.55 35.29 19.87 74.68
60.68 1.85 92.18 91.69 81.32 60.38 44.51

the command M = importdata('myfile.txt', ' ', 1) will give me a structure :
C =
          data: [5x3 double]
      textdata: {'Day1' 'Day2' 'Day3' 'Day4' 'Day5' 'Day6' 'Day7'}
    colheaders: {'Day1' 'Day2' 'Day3' 'Day4' 'Day5' 'Day6' 'Day7'}

But !!! if I use this text file :
Temps Tension-Dev1_ai0 Tension-Dev1_ai1 Tension-Dev1_ai2 Tension-Dev1_ai3
0.0000E+0 1.3184E-1 1.4648E-1 1.2695E-1 9.7656E-2
2.0000E-3 1.2695E-1 1.3184E-1 1.2695E-1 1.1719E-1
4.0000E-3 1.2695E-1 1.3184E-1 1.2695E-1 9.7656E-2

it will give me a 1x1 cell :
C =
    'Temps Tension-Dev1_ai0 Tension-Dev1_ai1 Tension-Dev1_ai2 Tension-Dev1_ai3'

So, what I need is a structure for the second text file, and I still can't get it.
I don't see where the problem is, as I don't see any differences in the two text files.

Please, help !!!!!

Thanks very much !!!

Subject: importdata problem !!

From: Aurelien Queffurust

Date: 17 Nov, 2010 12:31:04

Message: 2 of 8

"Egli Vorfi" <vig.notorious@gmail.com> wrote in message <ic0h40$5ti$1@fred.mathworks.com>...

> For example :
>
> If a use this text file :
>
> Day1 Day2 Day3 Day4 Day5 Day6 Day7
> 95.01 76.21 61.54 40.57 5.79 20.28 1.53
> 23.11 45.65 79.19 93.55 35.29 19.87 74.68
> 60.68 1.85 92.18 91.69 81.32 60.38 44.51
>
> the command M = importdata('myfile.txt', ' ', 1) will give me a structure :
> C =
> data: [5x3 double]
> textdata: {'Day1' 'Day2' 'Day3' 'Day4' 'Day5' 'Day6' 'Day7'}
> colheaders: {'Day1' 'Day2' 'Day3' 'Day4' 'Day5' 'Day6' 'Day7'}
>


euh there is problem even with your first test, the first answer is not expected : data should be [3x7 double] and not [5x3 double]

Aurélien

http://blog.developpez.com/matlab4geek/

Subject: importdata problem !!

From: Egli Vorfi

Date: 17 Nov, 2010 13:35:05

Message: 3 of 8

yeah you're right, it should a 3x7 double, it's just a copy paste mistake i did.

does the second text file work for you ??


"Aurelien Queffurust" <tug83@yahoo.fr> wrote in message <ic0hu7$rce$1@fred.mathworks.com>...
> "Egli Vorfi" <vig.notorious@gmail.com> wrote in message <ic0h40$5ti$1@fred.mathworks.com>...
>
> > For example :
> >
> > If a use this text file :
> >
> > Day1 Day2 Day3 Day4 Day5 Day6 Day7
> > 95.01 76.21 61.54 40.57 5.79 20.28 1.53
> > 23.11 45.65 79.19 93.55 35.29 19.87 74.68
> > 60.68 1.85 92.18 91.69 81.32 60.38 44.51
> >
> > the command M = importdata('myfile.txt', ' ', 1) will give me a structure :
> > C =
> > data: [5x3 double]
> > textdata: {'Day1' 'Day2' 'Day3' 'Day4' 'Day5' 'Day6' 'Day7'}
> > colheaders: {'Day1' 'Day2' 'Day3' 'Day4' 'Day5' 'Day6' 'Day7'}
> >
>
>
> euh there is problem even with your first test, the first answer is not expected : data should be [3x7 double] and not [5x3 double]
>
> Aurélien
>
> http://blog.developpez.com/matlab4geek/

Subject: importdata problem !!

From: Aurelien Queffurust

Date: 17 Nov, 2010 13:50:05

Message: 4 of 8

"Egli Vorfi" <vig.notorious@gmail.com> wrote in message <ic0lm9$qip$1@fred.mathworks.com>...
> yeah you're right, it should a 3x7 double, it's just a copy paste mistake i did.
>
> does the second text file work for you ??
>
ek/

Yes no problem within 10b , and to tell you the truth even if R12SP1 I get the same result:
 C=importdata('test.txt',' ',1)

C =

          data: [3x5 double]
      textdata: {'Temps' 'Tension-Dev1_ai0' 'Tension-Dev1_ai1' 'Tension-Dev1_ai2' 'Tension-Dev1_ai3'}
    colheaders: {'Temps' 'Tension-Dev1_ai0' 'Tension-Dev1_ai1' 'Tension-Dev1_ai2' 'Tension-Dev1_ai3'}

Subject: importdata problem !!

From: Egli Vorfi

Date: 17 Nov, 2010 14:47:03

Message: 5 of 8

ok, thanks a lot for your answer mate !


"Aurelien Queffurust" <tug83@yahoo.fr> wrote in message <ic0mid$nfd$1@fred.mathworks.com>...
> "Egli Vorfi" <vig.notorious@gmail.com> wrote in message <ic0lm9$qip$1@fred.mathworks.com>...
> > yeah you're right, it should a 3x7 double, it's just a copy paste mistake i did.
> >
> > does the second text file work for you ??
> >
> ek/
>
> Yes no problem within 10b , and to tell you the truth even if R12SP1 I get the same result:
> C=importdata('test.txt',' ',1)
>
> C =
>
> data: [3x5 double]
> textdata: {'Temps' 'Tension-Dev1_ai0' 'Tension-Dev1_ai1' 'Tension-Dev1_ai2' 'Tension-Dev1_ai3'}
> colheaders: {'Temps' 'Tension-Dev1_ai0' 'Tension-Dev1_ai1' 'Tension-Dev1_ai2' 'Tension-Dev1_ai3'}

Subject: importdata problem !!

From: Matthew Landowski

Date: 17 Nov, 2010 14:50:05

Message: 6 of 8

When I copy and pasted your file from you topic into a test file I got the 1x1 cell you spoke of without any data.

After playing around it appears to be a formating issue. I went through the file and deleted all the white space and reinserted spaces. After that change the file loaded perfectly.

I would check the formating in the files you are using. I believe some of the white space was not a space it was a tab and in the depths of ASCII there is a difference. And i'm not sure if the import function supports multiple delimiters. Someone correct me if i'm wrong.

If your file is too large to edit manually you might have to go with textscan or a similar function to pull your file in.

for the second case Ex.

fid = fopen('myfile.txt')

C_header = textscan(fid, '%s', 5) %5 is the # of columns that are headers
C_data = textscan(fid, '%f)

 fclose(fid)

textscan as default uses whitespace as a delimiter any tabs, spaces, etc are used to separate the data.

Hope this help!

-Matt

Subject: importdata problem !!

From: Egli Vorfi

Date: 17 Nov, 2010 15:32:03

Message: 7 of 8

thanks a lot for your help Matt !!

I remarked the space problem too, it turns to be a Tab space and when i changed it to a normal space, it worked. But my files are too large, so I tried this one :

[C1,C2,C3,C4,C5] = textread('test.txt','%f %f %f %f %f','headerlines',1);

and it works even with Tab spaces.
I don't know which is better to use, textread or textscan ?
Which do you think is better ?

Thanks again :)

"Matthew Landowski" <litho23@gmail.com> wrote in message <ic0q2s$b98$1@fred.mathworks.com>...
> When I copy and pasted your file from you topic into a test file I got the 1x1 cell you spoke of without any data.
>
> After playing around it appears to be a formating issue. I went through the file and deleted all the white space and reinserted spaces. After that change the file loaded perfectly.
>
> I would check the formating in the files you are using. I believe some of the white space was not a space it was a tab and in the depths of ASCII there is a difference. And i'm not sure if the import function supports multiple delimiters. Someone correct me if i'm wrong.
>
> If your file is too large to edit manually you might have to go with textscan or a similar function to pull your file in.
>
> for the second case Ex.
>
> fid = fopen('myfile.txt')
>
> C_header = textscan(fid, '%s', 5) %5 is the # of columns that are headers
> C_data = textscan(fid, '%f)
>
> fclose(fid)
>
> textscan as default uses whitespace as a delimiter any tabs, spaces, etc are used to separate the data.
>
> Hope this help!
>
> -Matt

Subject: importdata problem !!

From: Aurelien Queffurust

Date: 18 Nov, 2010 07:46:04

Message: 8 of 8

"Egli Vorfi" <vig.notorious@gmail.com> wrote in message <ic0shj$nmf$1@fred.mathworks.com>...

> I don't know which is better to use, textread or textscan ?
> Which do you think is better ?
>


As of 10a , you can read here:
http://www.mathworks.com/help/techdoc/rn/br_bpq8-1.html#bsdnyvb-1

Replace all existing instances of textread with textscan, similar to strread. Open and close files with fopen and fclose.

Aurélien

http://blog.developpez.com/matlab4geek/

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