Azzera filtri
Azzera filtri

Run a code multiple times and save variables

1 visualizzazione (ultimi 30 giorni)
Stuart
Stuart il 11 Lug 2014
Commentato: dpb il 12 Lug 2014
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

Risposta accettata

dpb
dpb il 11 Lug 2014
Modificato: dpb il 12 Lug 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 Commenti
Stuart
Stuart il 12 Lug 2014
Thanks dpb.
Much appreciated.
dpb
dpb il 12 Lug 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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Get Started with MATLAB in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by