Tuesday 15 January 2013

arrays - Extract values from text strings using Matlab's sscanf -



arrays - Extract values from text strings using Matlab's sscanf -

i have next string:

str={"usd":{"last":352.49,"bid":352.49,"ask":353.6, "high":358.75,"low":349.34,"volume":6678.7783}.....

and extract values in column vector this

colvector=[352.49,352.49,353.6,358.75,349.34,6678.7783]

i believe sscanf function can useful extract desirable format. tried different variations of using sscanf, haven't managed extract values string.

fmt=['{"usd":{"last": "%f", "bid": "%f", "ask": "%f", "high": "%f", "low": "%f", "volume": "%f"}...

for illustration colvector=sscanf(str,fmt) not working successfully.

assuming have string variable containing text,

s = 'str={"usd":{"last":352.49,"bid":352.49,"ask":353.6,"high":358.75,"low":349.34,"volume":6678.7783}.';

you can apply regular look isolate desired strings (consisting of digits followed optionally decimal point , digits). utilize regexp function 'match' option. produces cell array of strings. convert each string number using cellfun str2num. combining single line,

result = cellfun(@str2num, regexp(s, '\d+(\.\d+)?', 'match'));

for illustration string, produces:

result = 1.0e+003 * 0.3525 0.3525 0.3536 0.3588 0.3493 6.6788

arrays string matlab text

No comments:

Post a Comment