[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
how to convert a structure array of strings into a string array?
From: |
Thorsten Meyer |
Subject: |
how to convert a structure array of strings into a string array? |
Date: |
Fri, 17 Oct 2008 21:15:57 +0200 |
User-agent: |
Mozilla-Thunderbird 2.0.0.16 (X11/20080724) |
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.
=================================================
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
=================================================
then there is the str2mat function that does the trick:
octave:14> str2mat(a{:})
ans =
eins
zwei
drei
However, looking at its source, it is only a loop over the list of input
strings. Trying with a large array
a=cellstr(char(70+floor(20*rand(50000,100))));
the eval solution takes 5s, while the str2mat function takes 11.3s on my
computer (the cat function caused my octave to hang for that example).
Does anyone has a better idea?
thanks and regards
Thorsten Meyer
- how to convert a structure array of strings into a string array?,
Thorsten Meyer <=