How to transform struc/field data to single matrix with specific columns

2 ビュー (過去 30 日間)
Zahid
Zahid 2014 年 7 月 5 日
コメント済み: pietro 2014 年 7 月 8 日
Hi, I have downloaded some stock price data into matlab for certain tickers and receive the data in a struc/table based format instead of the excel style with all the columns for close price with the date. Is there a way to extract just the relevant close price filed data and form a matrix with close price columns for a few tickers
here is some sample code:
[temp, status] = urlread(strcat('http://ichart.finance.yahoo.com/table.csv?s='...
,tickers{i},'&a=',bm,'&b=',bd,'&c=',by,'&d=',em,'&e=',ed,'&f=',...
ey,'&g=',freq,'&ignore=.csv'));
if status
% organize data by using the comma delimiter
[date, op, high, low, cl, volume, adj_close] = ...
strread(temp(43:end),'%s%s%s%s%s%s%s','delimiter',',');
stocks(idx).Ticker = tickers{i}; % obtain ticker symbol
stocks(idx).Date = date; % save date data
stocks(idx).Open = str2double(op); % save opening price data
stocks(idx).High = str2double(high); % save high price data
stocks(idx).Low = str2double(low); % save low price data
stocks(idx).Close = str2double(cl); % save closing price data
stocks(idx).Volume = str2double(volume); % save volume data
stocks(idx).AdjClose = str2double(adj_close); % save adjustied close data
idx = idx + 1; % increment stock index
end
Thanks for looking into the issue, any help/advice greatly appreciated,
Mark

回答 (2 件)

pietro
pietro 2014 年 7 月 5 日
What about using struct2cell?
  4 件のコメント
Zahid
Zahid 2014 年 7 月 6 日
Sure, the final output should be an array corresponding to the longest date with each tickers close values (if we have 4 tickers then an array with 5 columns and rows linked to the number of values for each column)
Date Close1 Close2 Close3 Close4
06/07/2014 4 5 8 2 ..... .. .. .. ... ..... .. .. ... ... 01/01/1987 6 7 8 3
I tried to construct a loop too but only populated the first column and received a mismatch error
EDU>> h = {}; EDU>> stocks = hist_stock_data('01011987', '04072014', '^FTSE','^FTAS','^FTMC','^FTLC',4); EDU>> n = 4; EDU>> for i = 1:n h(:,i) = stocks(i).Close; end Subscripted assignment dimension mismatch.
pietro
pietro 2014 年 7 月 8 日
Please be more specific, provide a numerical example like with the input and the outpu. something like:
p=struct('f1',[],'f2',[],'f3',[]);
p(1).f1=1
p(1).f2='20/05/2012';
....
output=.....
Otherwise I can't really help you.

サインインしてコメントする。


Zahid
Zahid 2014 年 7 月 5 日
I had a look into the struct2cell and cell2mat ; using struct2cell gave back
val(:,:,1) =
'^FTSE'
{6651x1 cell }
[6651x1 double]
[6651x1 double]
[6651x1 double]
[6651x1 double]
[6651x1 double]
[6651x1 double]
val(:,:,2) =
'^FTAS'
{6463x1 cell }
[6463x1 double]
[6463x1 double]
[6463x1 double]
[6463x1 double]
[6463x1 double]
[6463x1 double]
val(:,:,3) =
'^FTMC'
{6516x1 cell }
[6516x1 double]
[6516x1 double]
[6516x1 double]
[6516x1 double]
[6516x1 double]
[6516x1 double]
val(:,:,4) =
'^FTLC'
{6514x1 cell }
[6514x1 double]
[6514x1 double]
[6514x1 double]
[6514x1 double]
[6514x1 double]
[6514x1 double]
but i still couldn't extract the date with the 6th column data from each of the four tickers to give a single matrix with approx 6600 by 5 cols

カテゴリ

Help Center および File ExchangeFinancial Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by