Run a code multiple times and save variables

1 次查看(过去 30 天)
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

采纳的回答

dpb
dpb 2014-7-11
编辑:dpb 2014-7-12
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 个评论
dpb
dpb 2014-7-12
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by