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:
help: stran cell array

Subject: help: stran cell array

From: Alessandra

Date: 10 Jul, 2010 12:49:21

Message: 1 of 20

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

Subject: help: stran cell array

From: dpb

Date: 10 Jul, 2010 13:20:24

Message: 2 of 20

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)

--

Subject: help: stran cell array

From: Faraz Afzal

Date: 10 Jul, 2010 13:31:07

Message: 3 of 20

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

Subject: help: stran cell array

From: Faraz Afzal

Date: 10 Jul, 2010 13:45:22

Message: 4 of 20

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

Subject: help: stran cell array

From: Faraz Afzal

Date: 10 Jul, 2010 14:27:04

Message: 5 of 20

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

Subject: help: stran cell array

From: us

Date: 10 Jul, 2010 19:32:05

Message: 6 of 20

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

Subject: help: stran cell array

From: Alessandra

Date: 10 Jul, 2010 20:26:27

Message: 7 of 20

Hi dpb,

I tried your method, it works indeed but only on the elements I input in v, namely:

s={'128 ';' 1 ';' 50 '; ' 75 '};
v=zeros(size(s));
for idx=1:length(s), v(idx)=str2num(s{idx});
end

I need that the sequence of the stimuli is respected as it appears on my file. There are more than 1000 data point in my coloumn, I would need to input the whole coloumn if you know what I mean.

My array is stim and I tried to do soemthing like s={stim};
but it prints this:
  {1403x1 cell}
and if I run your script I get this error:

>> s={stim};
>> v=zeros(size(s));
for idx=1:length(s), v(idx)=str2num(s{idx});end
??? Error using ==> str2num at 33
Requires string or character array input.

This is a very annoying problem..
Thanks a lot guys for helping me!

Alessandra

Subject: help: stran cell array

From: us

Date: 10 Jul, 2010 21:01:07

Message: 8 of 20

Alessandra <danda.galli@gmail.com> wrote in message <1217621656.7454.1278793617867.JavaMail.root@gallium.mathforum.org>...
> Hi dpb,
>
> I tried your method, it works indeed but only on the elements I input in v, namely:
>
> s={'128 ';' 1 ';' 50 '; ' 75 '};
> v=zeros(size(s));
> for idx=1:length(s), v(idx)=str2num(s{idx});
> end
>
> I need that the sequence of the stimuli is respected as it appears on my file. There are more than 1000 data point in my coloumn, I would need to input the whole coloumn if you know what I mean.
>
> My array is stim and I tried to do soemthing like s={stim};
> but it prints this:
> {1403x1 cell}
> and if I run your script I get this error:
>
> >> s={stim};
> >> v=zeros(size(s));
> for idx=1:length(s), v(idx)=str2num(s{idx});end
> ??? Error using ==> str2num at 33
> Requires string or character array input.
>
> This is a very annoying problem..
> Thanks a lot guys for helping me!
>
> Alessandra

did you try the ...other... solution(?)...

us

Subject: help: stran cell array

From: Alessandra

Date: 10 Jul, 2010 21:17:25

Message: 9 of 20

well,
this solution:

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)

gives me the array r with all sort of other numbers, I think it mixes up the two coloumn of my vector. It's not quote it.

I give you the first rows of my txt file, it might help:

R128 90.087400
R128 92.087400
R128 94.087400
R128 96.087400
S254 96.100200
R128 98.087400
S 50 99.115600
R128 100.087400
S 50 101.181200
R128 102.087400
S 50 103.263600
R128 104.087400
S 50 105.329400
R128 106.087400
S100 107.411800
R128 108.087400
S 50 109.494200
R128 110.087400
S 50 111.576600
R128 112.087400
S 50 113.642200
R128 114.087400
S 75 115.724600

you see, in the first coloumn I have the stimuli or markers and in the second the time when they appear.

Alessandra

Subject: help: stran cell array

From: us

Date: 10 Jul, 2010 21:39:05

Message: 10 of 20

Alessandra <danda.galli@gmail.com> wrote in message <658936373.7743.1278796675389.JavaMail.root@gallium.mathforum.org>...
> well,
> this solution:
>
> 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)
>
> gives me the array r with all sort of other numbers, I think it mixes up the two coloumn of my vector. It's not quote it.
>
> I give you the first rows of my txt file, it might help:
>
> R128 90.087400
> R128 92.087400
> R128 94.087400
> R128 96.087400

> you see, in the first coloumn I have the stimuli or markers and in the second the time when they appear.
>
> Alessandra

it is pretty stupid to not give the correct anatomy of your file's content in your OP and, therefore, carelessly waste CSSMers' time(!!!!!)...

in your OP you showed this pattern...
S 75
S100
S 50
S 1
S128

be considerate when you post...

us

Subject: help: stran cell array

From: us

Date: 10 Jul, 2010 21:48:05

Message: 11 of 20

"us "

one of the solutions
- just in case you got your act together...

     fnam='foo.txt'; % <- your file name...
     s=textread(fnam,'%s','delimiter','\n');
     s=regexprep(s,'[A-z]','');
     r=cellfun(@(x) sscanf(x,'%g %g'),s,'uni',false);
     r=cat(2,r{:}).'
%{
% r = % <- 10 records, only...
     128.0000 90.0874
     128.0000 92.0874
     128.0000 94.0874
     128.0000 96.0874
     254.0000 96.1002
     128.0000 98.0874
      50.0000 99.1156
     128.0000 100.0874
      50.0000 101.1812
     128.0000 102.0874
%}

us

Subject: help: stran cell array

From: dpb

Date: 11 Jul, 2010 00:03:28

Message: 12 of 20

Alessandra wrote:
...

> and if I run your script I get this error:
>
>>> s={stim};
>>> v=zeros(size(s));
> for idx=1:length(s), v(idx)=str2num(s{idx});end
> ??? Error using ==> str2num at 33
> Requires string or character array input.
...

Well, I presumed you would have the common sense to use your cell array
instead of just blindly copying a quickie keyboard demonstration... :(

I'm quite sure if contents of stim are the character strings as valid
numbers that

v=zeros(size(stim));
for idx=1:length(stim)
   v(idx)=str2num(stim{idx});
end

will work as intended.

However, as I noted earlier, w/ cellfun and later version than I have,
there's probably a better way as us has shown.

--

Subject: help: stran cell array

From: Faraz Afzal

Date: 11 Jul, 2010 00:23:05

Message: 13 of 20

dpb <none@non.net> wrote in message <i1b1tb$inn$1@news.eternal-september.org>...
> Alessandra wrote:
> ...
>
> > and if I run your script I get this error:
> >
> >>> s={stim};
> >>> v=zeros(size(s));
> > for idx=1:length(s), v(idx)=str2num(s{idx});end
> > ??? Error using ==> str2num at 33
> > Requires string or character array input.
> ...
>
> Well, I presumed you would have the common sense to use your cell array
> instead of just blindly copying a quickie keyboard demonstration... :(
>
> I'm quite sure if contents of stim are the character strings as valid
> numbers that
>
> v=zeros(size(stim));
> for idx=1:length(stim)
> v(idx)=str2num(stim{idx});
> end
>
> will work as intended.
>
> However, as I noted earlier, w/ cellfun and later version than I have,
> there's probably a better way as us has shown.
>
> --

Well I wonder why you people started insulting someone who made a mistake.. Were you guys never making mistakes.. Its of my no concern but I would just say You,me and all of US here are to help each other, then help if its of concern else leave it..

I am sorry I just hated the attitude of the perfect guys here..

Regards,
Faraz

Subject: help: stran cell array

From: us

Date: 11 Jul, 2010 00:34:05

Message: 14 of 20

"Faraz Afzal"
> Well I wonder why you people started insulting someone who made a mistake.. Were you guys never making mistakes.. Its of my no concern but I would just say You,me and all of US here are to help each other, then help if its of concern else leave it..
>
> I am sorry I just hated the attitude of the perfect guys here..
>
> Regards,
> Faraz

well...
YOU obviously have not wasted YOUR time to (even try to) come up with a solution...
words are cheap - yet ML code is better...
...and i have not seen YOU come up with a single line of ML language...

us

Subject: help: stran cell array

From: Faraz Afzal

Date: 11 Jul, 2010 00:50:09

Message: 15 of 20

"us " <us@neurol.unizh.ch> wrote in message <i1b3ht$a1p$1@fred.mathworks.com>...
> "Faraz Afzal"
> > Well I wonder why you people started insulting someone who made a mistake.. Were you guys never making mistakes.. Its of my no concern but I would just say You,me and all of US here are to help each other, then help if its of concern else leave it..
> >
> > I am sorry I just hated the attitude of the perfect guys here..
> >
> > Regards,
> > Faraz
>
> well...
> YOU obviously have not wasted YOUR time to (even try to) come up with a solution...
> words are cheap - yet ML code is better...
> ...and i have not seen YOU come up with a single line of ML language...
>
> us

:))

Much expected proudy behavior.. 'us' u rock !!! :))) hatss off !!!

Regards,
Faraz

Subject: help: stran cell array

From: Alessandra

Date: 11 Jul, 2010 11:51:45

Message: 16 of 20

Faraz,

Thanks for writing this,
I found the "us" reply very coarse and unpolite
I don't particulary mind, there are also people like this..

By the way I solved my problem, I followed the dpb suggestion I could finally have my double array.

Thanks guys for yur help,

Ale

Subject: help: stran cell array

From: Alessandra

Date: 11 Jul, 2010 11:56:52

Message: 17 of 20

..us, nobody forced you to reply to my message, you chose to "waste your time". Insulting around doesn't make you certainly cool nor gives you back "all the time" you have spent finding a possible solution.

Regards,

Alessandra

Subject: help: stran cell array

From: us

Date: 11 Jul, 2010 12:19:04

Message: 18 of 20

Alessandra <danda.galli@gmail.com> wrote in message <772837588.11037.1278849442590.JavaMail.root@gallium.mathforum.org>...
> ..us, nobody forced you to reply to my message, you chose to "waste your time". Insulting around doesn't make you certainly cool nor gives you back "all the time" you have spent finding a possible solution.
>
> Regards,
>
> Alessandra

yep, you just confirm what i told you earlier...
you obviously lack any insight into what you're doing - and how you treat people...
not boding well for your future here at CSSM...

us

Subject: help: stran cell array

From: us

Date: 11 Jul, 2010 12:22:04

Message: 19 of 20

Alessandra <danda.galli@gmail.com> wrote in message <1048097875.10998.1278849135276.JavaMail.root@gallium.mathforum.org>...
> Faraz,
>
> Thanks for writing this,
> I found the "us" reply very coarse and unpolite
> I don't particulary mind, there are also people like this..
>
> By the way I solved my problem, I followed the dpb suggestion I could finally have my double array.

unless you show CSSM your solution (based on DPB's suggestion), people will not trust you...

us

Subject: help: stran cell array

From: dpb

Date: 11 Jul, 2010 14:00:54

Message: 20 of 20

Faraz Afzal wrote:
...

> Well I wonder why you people started insulting someone who made a
> mistake.. Were you guys never making mistakes.. Its of my no concern but
> I would just say You,me and all of US here are to help each other, then
> help if its of concern else leave it..
>
> I am sorry I just hated the attitude of the perfect guys here..
...

Well, helping is what was done, isn't it?

It only seems reasonable that those being helped make at least token
efforts to solve their problems and learn on their own.

A little encouragement in that vein when what would seem to quite
clearly have been simply a demonstration of a basic technique wasn't
tailored at all to the specific problem by simply the substitution of
the local variable name seemed appropriate...

The complaint that a solution to a posted data format or specific
example "doesn't work" because the actual data aren't of that form (or
are a special case or subset of the more general form) is quite common,
unfortunately, also. That one should, in fact, ask the question one
wants/needs an answer to is also a general tenet of cs-sm. Some need
more reminding than others; the gentleness of the reminder often has
much to do w/ the wording of the complaint; the respondent also will
always have their own quirks as well as any special circumstances of the
time...

There were no personal insults; there was some level of shall I say
acerbity, granted.

--

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