Run a code multiple times and save variables

1 view (last 30 days)
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

Accepted Answer

dpb
dpb on 11 Jul 2014
Edited: dpb on 12 Jul 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 Comments
dpb
dpb on 12 Jul 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.

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!