help: stran cell array

  • Follow


Hi There,

I cannot menage to convert an array cell into double.
I'll expain you better. I imported a txt file of two coloums, the first is the problematic one, it looks like this:

S 75
S100
S 50
S 1
S128
etc..

Some elements have a space, some two spaces some none.
While importing I used textread and this syntax that allowed me to split the "numeric" part of these elements from the "Character" part.
My two coloumns are stim and time:

[trash, stim, time] = textread(files, '%c %s %f', 'delimiter','\t', 'whitespace', '', 'headerlines',2);
clear trash

This works out fine but one I have my stim array this is saved as cell and if I print it, it looks like this:

'128 '
' 1'
' 50'
etc..

I need to convert this new array of only numbers although treated as cell into a double so that I can creat a matrix with stim and time as coloumn. I need this since the stim variable has markers that I need to filter the time variable.

I hope it was clear, I can't find a solution for this, could you help me?

Thanks a lot

Ale
0
Reply danda.galli (1) 7/10/2010 7:49:21 AM

Alessandra wrote:
> Hi There,
> 
> I cannot menage to convert an array cell into double.
....
> ... stim array ... as cell and ... looks like this:
> 
> '128 '
> ' 1'
> ' 50'
> etc..
> 
> I need to convert ... into a double ...
....

AFAIK there's still no way other than the loop solution...

 >> s={'128 ';' 1';' 50'};
 >> v=zeros(size(s);
 >> v=zeros(size(s));
 >> for idx=1:length(s), v(idx)=str2num(s{idx});end
 >> v
v =
    128
      1
     50
 >> whos v
   Name      Size         Bytes  Class

   v         3x1             24  double array

Grand total is 3 elements using 24 bytes

 >>

There are some more advanced cell operations/functions in releases later 
than mine so perhaps there is a solution...mayhaps one could now use 
cellfun()??? (but it's only an limited early precursor in R12 so can't 
experiment)

--
0
Reply dpb 7/10/2010 1:20:24 PM


Alessandra <danda.galli@gmail.com> wrote in message <1075412655.5267.1278766191278.JavaMail.root@gallium.mathforum.org>...
> Hi There,
> 
> I cannot menage to convert an array cell into double.
> I'll expain you better. I imported a txt file of two coloums, the first is the problematic one, it looks like this:
> 
> S 75
> S100
> S 50
> S 1
> S128
> etc..
> 
> Some elements have a space, some two spaces some none.
> While importing I used textread and this syntax that allowed me to split the "numeric" part of these elements from the "Character" part.
> My two coloumns are stim and time:
> 
> [trash, stim, time] = textread(files, '%c %s %f', 'delimiter','\t', 'whitespace', '', 'headerlines',2);
> clear trash
> 
> This works out fine but one I have my stim array this is saved as cell and if I print it, it looks like this:
> 
> '128 '
> ' 1'
> ' 50'
> etc..
> 
> I need to convert this new array of only numbers although treated as cell into a double so that I can creat a matrix with stim and time as coloumn. I need this since the stim variable has markers that I need to filter the time variable.
> 
> I hope it was clear, I can't find a solution for this, could you help me?
> 
> Thanks a lot
> 
> Ale

Hi Alessandra, 

There u go...

A = cat(1,stim{:})
A = double(E)

Let me know if it worked..

Regards,
Muhammad Faraz 
0
Reply Faraz 7/10/2010 1:31:07 PM

dpb <none@non.net> wrote in message <i19s7k$620$1@news.eternal-september.org>...
> Alessandra wrote:
> > Hi There,
> > 
> > I cannot menage to convert an array cell into double.
> ...
> > ... stim array ... as cell and ... looks like this:
> > 
> > '128 '
> > ' 1'
> > ' 50'
> > etc..
> > 
> > I need to convert ... into a double ...
> ...
> 
> AFAIK there's still no way other than the loop solution...
> 
>  >> s={'128 ';' 1';' 50'};
>  >> v=zeros(size(s);
>  >> v=zeros(size(s));
>  >> for idx=1:length(s), v(idx)=str2num(s{idx});end
>  >> v
> v =
>     128
>       1
>      50
>  >> whos v
>    Name      Size         Bytes  Class
> 
>    v         3x1             24  double array
> 
> Grand total is 3 elements using 24 bytes
> 
>  >>
> 
> There are some more advanced cell operations/functions in releases later 
> than mine so perhaps there is a solution...mayhaps one could now use 
> cellfun()??? (but it's only an limited early precursor in R12 so can't 
> experiment)
> 
> --

Hi , 
OK my method won't work..
I just noticed u have data stored in cells as strings, 
in other case cell2mat would also work..

Now i would say they way told by dpb is one of the solutions...
Regards,
Muhammad Faraz
0
Reply Faraz 7/10/2010 1:45:22 PM

Alessandra <danda.galli@gmail.com> wrote in message <1075412655.5267.1278766191278.JavaMail.root@gallium.mathforum.org>...
> Hi There,
> 
> I cannot menage to convert an array cell into double.
> I'll expain you better. I imported a txt file of two coloums, the first is the problematic one, it looks like this:
> 
> S 75
> S100
> S 50
> S 1
> S128
> etc..
> 
> Some elements have a space, some two spaces some none.
> While importing I used textread and this syntax that allowed me to split the "numeric" part of these elements from the "Character" part.
> My two coloumns are stim and time:
> 
> [trash, stim, time] = textread(files, '%c %s %f', 'delimiter','\t', 'whitespace', '', 'headerlines',2);
> clear trash
> 
> This works out fine but one I have my stim array this is saved as cell and if I print it, it looks like this:
> 
> '128 '
> ' 1'
> ' 50'
> etc..
> 
> I need to convert this new array of only numbers although treated as cell into a double so that I can creat a matrix with stim and time as coloumn. I need this since the stim variable has markers that I need to filter the time variable.
> 
> I hope it was clear, I can't find a solution for this, could you help me?
> 
> Thanks a lot
> 
> Ale

Finally A better solution is to directly extract numerical data from text and then use catenation..

fid = fopen('YOUR_FILE.txt');
A = textscan(fid, ' %1s %f32 ')
fclose(fid)
and A{2} contains all numeric data not strings...

Regards, 
Muhammad Faraz
0
Reply Faraz 7/10/2010 2:27:04 PM

Alessandra <danda.galli@gmail.com> wrote in message <1075412655.5267.1278766191278.JavaMail.root@gallium.mathforum.org>...
> Hi There,
> 
> I cannot menage to convert an array cell into double.
> I'll expain you better. I imported a txt file of two coloums, the first is the problematic one, it looks like this:
> 
> S 75
> S100
> S 50
> S 1
> S128
> etc..
> 
> Some elements have a space, some two spaces some none.
> While importing I used textread and this syntax that allowed me to split the "numeric" part of these elements from the "Character" part.
> My two coloumns are stim and time:
> 
> [trash, stim, time] = textread(files, '%c %s %f', 'delimiter','\t', 'whitespace', '', 'headerlines',2);
> clear trash
> 
> This works out fine but one I have my stim array this is saved as cell and if I print it, it looks like this:
> 
> '128 '
> ' 1'
> ' 50'
> etc..
> 
> I need to convert this new array of only numbers although treated as cell into a double so that I can creat a matrix with stim and time as coloumn. I need this since the stim variable has markers that I need to filter the time variable.
> 
> I hope it was clear, I can't find a solution for this, could you help me?
> 
> Thanks a lot
> 
> Ale

one of the many solutions
- given the anatomy of your file...

     fnam='foo.txt';     % <- your file name...
     s=textread(fnam,'%s','delimiter','\n');
     s=regexp(s,'\d+','match');
     r=cellfun(@(x) sscanf(x,'%g'),[s{:}],'uni',false)
%    r = [75]    [100]    [50]    [1]    [128]

us
0
Reply us 7/10/2010 7:32:05 PM

5 Replies
179 Views

(page loaded in 0.217 seconds)

Similiar Articles:





7/26/2012 11:04:35 AM


Reply: