Run a code multiple times and save variables

Hi,
I would like to run the following code every 20 seconds for 5 minutes in order to get the latest bitcoin price.
rawdata = urlread('https://www.bitstamp.net/api/ticker/');
j = strfind(rawdata, 'high');
k = strfind(rawdata, 'last');
m = strfind(rawdata, 'vwap');
o = strfind(rawdata, 'volume');
p = strfind(rawdata, 'low');
High=rawdata(j+8:j+13);
Last=rawdata(k+8:k+13)
vwap=rawdata(m+8:m+13);
volume=rawdata(o+10:o+22);
low=rawdata(p+7:p+12);
I would like the value of high/last/vwap etc for each of the time periods. I am having trouble using a vector as I don't know if urlread can be used in this format.
Any help would be much appreciated.
Thanks,
Stu

 Respuesta aceptada

dpb
dpb el 11 de Jul. de 2014
Editada: dpb el 12 de Jul. de 2014
M=5; % number of minutes
P=20; % period between readings
N=60/P*M; % readings expected
% format string for the data line returned...
fmt=['{"high": "%f", "last": "%f",' repmat('%*s ',1,4) '"vwap": "%f", "volume": "%f", "low": "%f"']
data=zeros(N,5); % preallocate
for i=1:N
rawdata=urlread('https://www.bitstamp.net/api/ticker/');
data(i,:)=sscanf(rawdata,fmt).';
pause(P)
end
Your values will be
High Last VWAP Vol Low
by column for each period.

4 comentarios

Thanks for answering so quick.Its much appreciated. When I run the above I receive the following error:
??? Subscripted assignment dimension mismatch.
Error in ==> dpb at 12
data(i,:)=sscanf(fmt,rawdata)';
Do you know why?
Thanks,
Stu
Check on rawdata being valid and what does
sscanf(fmt,rawdata)'
return at the command line?
Here with a sample line downloaded I get
>> rawdata
rawdata =
{"high": "635.00", "last": "633.00", "timestamp": "1405114465", "bid": "633.00", "vwap": "625.52", "volume": "6238.10039398", "low": "613.46", "ask": "634.78"}
>> sscanf(rawdata,fmt).'
ans =
1.0e+03 *
0.6350 0.6330 0.6255 6.2381 0.6135
Ewww.... :( My bad, somehow what I posted here has the arguments to sscanf reversed--should be
data(i,:)=sscanf(rawdata,fmt).';
I edited Answer to make the correction.
Stuart
Stuart el 12 de Jul. de 2014
Thanks dpb.
Much appreciated.
dpb
dpb el 12 de Jul. de 2014
No problem...you'll undoubtedly want to add some error handling and all...the pause is klunky; not sure whether there's a way to schedule it as a callback or not--never investigated that in Matlab.

Iniciar sesión para comentar.

Más respuestas (0)

Productos

Preguntada:

el 11 de Jul. de 2014

Comentada:

dpb
el 12 de Jul. de 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by