I'm wondering whether people would consider this function (which I'm
calling "strscrape" for now) worth having: it takes a string argument
and returns a row vector containing any numerical values encountered
in the string (or an empty matrix if none are found). There's a test
version in git but it's not documented; I'm not committed to keeping
it if doesn't attract much support.
Unlike using sscanf() to retrieve a matrix, the idea with this
function is that the numbers may be embedded in arbitrary text.
Numbers are identified as starting with either a minus sign (followed
directly by a digit) or a digit, and their length in characters is
decided by the C function strtod (so scientific notation is
acceptable). As the function stands, the decimal character is
unconditionally '.'.
One use for such a function would be "remedial" -- getting values back
from a gretl command when there's no relevant accessor. Here's a
semi-general illustration:
<hansl>
function matrix scrapevals (const string cmd, const list L[null])
string fname = sprintf("%s/_tmp.txt", $dotdir)
set force_decpoint on
outfile @fname --write --quiet
@cmd
outfile --close
string s = readfile(fname)
remove(fname)
return strscrape(s)
end function
open data4-10
list L = ENROLL CATHOL
matrix m = scrapevals("corr L --kendall", L)
print m
</hansl>
However, it could also be useful for processing text other than
gretl's own output. Here's a generic example:
<hansl>
string s="Here is some text 1.5e-4with embedded 12345 numbers \
stuck int0 1t."
matrix vals = strscrape(s)
print vals
</hansl>
which gives
vals (1 x 4)
0.00015000 12345. 0.0000 1.0000
Allin