convert nested struct to matrix

8 views (last 30 days)
Carsten
Carsten on 25 Sep 2019
Edited: Carsten on 7 Oct 2019
Hi
I have the following struct and would like to convert Stocks_10.Close into a matrix for vectorization
The following worked for equal fields.
stocks_10_cell = [stocks_10.Close]';
The issue is the data in Number 8, insted 757x1, it's 668x1.
I got the error:
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Does someone have a solution?
Thank you
Carsten
  1 Comment
Dheeraj Singh
Dheeraj Singh on 3 Oct 2019
Edited: Dheeraj Singh on 3 Oct 2019
You can try appending dummy values (0 or NaNs) at the end to make all of them of the same size and then try to convert into matrix.

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 3 Oct 2019
Well, you obviously can't concatenate vectors of dfferent sizes so if you want to do that you either have to trim the longer vectors or pad the shorter ones. Both of which are doable, but may mess up whatever calculation you want to do.
Instead, Perhaps the best approach would be to flatten all your data into a single table or timetable. You can then use groupsummary or similar functions to perform statistics according to groupings of your choosing:
%conversion of the structure to table:
%first replicate tickers so they're the same height as the other fields
%also convert Date to datetime as it will make life much easier
for si = 1:numel(Stocks_10)
Stocks_10(si).Ticker = repmat(Stocks_10(si).Ticker, size(Stocks_10(si).Open));
Stocks_10(si).Date = datetime(Stocks_10(si).Date); %may need extra options to perform the conversion properly
end
%then concatenate the whole lot and convert to table:
varnames = fiednames(Stocks_10);
stock_cell = cell(1, numel(varnames));
for fi = 1:numel(varnames)
stock_cell{fi} = vertcat(Stocks.(varnames(fi)));
end
stock_table = table(stock_cell{:}, 'VariableNames', varnames);
From them one, it's easy to do some stats, e.g. monthly mean of the variables per ticker:
ticker_monthlymean = groupsummary(stock_table, {'Date', 'Ticker'}, {'month', 'none'}, 'mean');
Or monthly mean regardless of ticker:
monthly_mean = groupsummary(stock_table, 'Date', 'month', 'mean', 2:width(stock_table)-1);
  1 Comment
Carsten
Carsten on 4 Oct 2019
Edited: Carsten on 7 Oct 2019
Thanks Guillaume
I’m thinking how to do it without increasing the cpu time, because that’s why I started the transformation from struct to matrix.
It works perfectly fine with 2 for loops directly with the structure. In this way I have full control as I can just start find the Index for the same date and then start with that Index for calculation.
If I transfer the data into a matrix I might get sometimes a stock which had an IPO and data is missing in the past, but starts at index 1.
Actually I have to take care on this as you suggested.
I think I have to program a small example and compare both options and compare the cpu time.
I let this question open for the moment and come back with the two examples for discussion.
From a first try out I get the impression it’s quite the same...
It would be nice if I could operate similar with a structure as with a matrix.
Thanks

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!