how to convert a structure array of strings into a string array?
David Bateman
dbateman at dbateman.org
Sat Oct 18 01:03:22 CDT 2008
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 dbateman at dbateman.org
35 rue Gambetta +33 1 46 04 02 18 (Home)
92100 Boulogne-Billancourt FRANCE +33 6 72 01 06 33 (Mob)
More information about the Help-octave
mailing list