help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: how to convert a structure array of strings into a string array?


From: David Bateman
Subject: Re: how to convert a structure array of strings into a string array?
Date: Sat, 18 Oct 2008 07:03:22 +0100
User-agent: Mozilla-Thunderbird 2.0.0.16 (X11/20080724)

Thorsten Meyer wrote:
Hi,

I would like to convert a large structure array of strings into a string
array in a nicely vectorized way.

for example:
octave:15> a={"eins", "zwei", "drei"};

should be converted into
ans =

eins
zwei
drei

=================================================
My first attempt was:


octave:16> a{:}
ans =

(,
  [1] = eins
  [2] = zwei
  [3] = drei
,)
Very nice: this generates a list of strings.

octave:17> [a{:}]
ans = einszweidrei
Here, the elements of the list are concatenated in the wrong way. :-(
=================================================

The cat function can concatenate correctly:

octave:69> cat(1, a{:})
ans =

eins
zwei
drei

However, it is incredibly slow for larger arrays and it cannot handle
strings of different length.

Its faster in 3.1.51+ and is as fast as [a{:}] now.


=================================================

This works, but I suppose it's rather slow for large arrays (also it is
not robust if the strings contains things like "):
octave:28> eval(["[", sprintf("""%s"";", a{:}), "]"])
ans =

eins
zwei
drei

I'd probably suggest this way till the a 3.2 release is made, then use cat.

D.

--
David Bateman                                address@hidden
35 rue Gambetta                              +33 1 46 04 02 18 (Home)
92100 Boulogne-Billancourt FRANCE            +33 6 72 01 06 33 (Mob)


reply via email to

[Prev in Thread] Current Thread [Next in Thread]