Reading and Combining Excel Time Series in Matlab- Maintaining Order -
i have following code read off time series data (contained in sheets 5 19 in excel workbook). each worksheet titled "ts" followed number of time series. process works fine apart 1 thing- when study returns find time series shifted along 5. i.e. ts 6 becomes 11th column in "returns" data , ts 19 becomes 5th column, ts 15 becomes 1st column etc. need them in same order read- such ts 1 in 1st column, ts 2 in 2nd etc. problem because read off titles of worksheets ("assetlist") maintain actual order throughout subsequent codes. therefore when recombine titles , returns find not match. complicates further manipulation when, example column 4 titled "ts 4" contains data of ts 18. there in code have wrong?
xl='timeseries.xlsx'; formatin = 'dd/mm/yyyy'; formatout = 'mmm-dd-yyyy'; bounds=3; [bounds,~] = xlsread(xl,bounds);
% determine number of worksheets in xls-file:
firstsheet=5; [~,assetlist] = xlsfinfo(xl); lngth=size(assetlist,2); assetlist(:,1:firstsheet-1)=[];
% loop through number of sheets , retrieve values
merge_count = 1; i=firstsheet:lngth [fundvalues, ~, fundsheet] = xlsread(xl,i);
% extract dates , data , combine % (to remove unneccessary text in rows 1 4)
fund_dates_data = fundsheet(4:end,1:2); funddates = cellstr(datestr(datevec(fund_dates_data(:,1),... formatin),formatout)); funddata = cell2mat(fund_dates_data(:,2)); % create time series each fund fundts{i}=fints(funddates,funddata,['fund',num2str(i)]); if merge_count == 2 port = merge(fundts{i-1},fundts{i},'datesetmethod','intersection'); end if merge_count > 2 port = merge(port,fundts{i},'datesetmethod','intersection'); end merge_count = merge_count + 1; end
% analyse portfolio
returns=tick2ret(port); q = portfolio; q = q.estimateassetmoments(returns) [qassetmean, qassetcovar] = q.getassetmoments
this due merge
. default, sorts columns alphabetically. unfortunately, naming pattern "fundn", means that, example, fund10 sorted before fund9. you're looping on 5 19, have fund10
, through fund19
, followed fund4
through fund9
.
one way of solving use 0 padding (fund01, fund02, etc) alphabetical order , numerical order same. alternatively, force stay in order read/merge data setting sortcolumns 0:
port = merge(port,fundts{i},'datesetmethod','intersection','sortcolumns',0);
Comments
Post a Comment