MATLAB Answers

0

How can I access results from loop, that are stored in structs within a cell?

Asked by Bárbara Ferreira on 1 Sep 2019
Latest activity Commented on by Bárbara Ferreira on 5 Sep 2019
Accepted Answer by dpb
Good afernoon,
I have a scrip built to call the same function (named QR) several times (with a loop) and to store it's results into structs within a cell:
first_obs = 100;
last_obs = 110;
results_1 = cell(last_obs,1);
dvar_i = bank_return;
dvar_j = system_return;
ivar = [a b c d e f];
quantile = 0.01;
for i=first_obs:last_obs
[coeff_i, fval_i, t_i, VaR, coeff_j, fval_j, t_j, CoVaR] = QR(dvar_i(1:i), dvar_j(1:i), ivar(1:i,:), quantile, 0);
results{i} = struct('coeff_i', coeff_i', 'fval_i', fval_i, 't_i', t_i', 'VaR', VaR, 'coeff_j', coeff_j', 'fval_j', fval_j, 't_j', t_j', 'CoVaR', CoVaR);
end
results = results';
How can I compare the variables' values obtained in each iteration of the loop?
I have already tried several functions, namely the struct2table and then the cell2table. However, I always get the a first table that have other tables within:
Therefore, I cannot look at all variables' values at the same time.
How can I build a matrix where the rows are the observations (each interation of the loop) and the columns are the values of each variable? Please note that the variables don't have the same size. You can find attached a table that show the way I need my matrix to be.
Thanks in advance!

  0 Comments

Sign in to comment.

Tags

1 Answer

Answer by dpb
on 1 Sep 2019
 Accepted Answer

first_obs = 100;
last_obs = 110;
Nobs=last_obs-first_obs+1; % number rows to compute
coeff_i=zeros(Nobs,3); % preallocate outputs
fval_1=zeros(Nobs,1);
t_i=coeff_i;
VaR=fval_1;
coeff_j=coeff_i;
fval_j=fval_i;
t_j=coeff_i;
CoVaR=fval_1;
dvar_i = bank_return;
dvar_j = system_return;
ivar = [a b c d e f];
quantile = 0.01;
j=0; % initialize counter for loop
for i=first_obs:last_obs
j=j+1; % increment counter
[coeff_i(j,:), fval_i(j,:), t_i(j,:), VaR(j,:), coeff_j(j,:), fval_j(j,:), t_j(j,:), CoVaR(j,:)] = ...
QR(dvar_i(1:i), dvar_j(1:i), ivar(1:i,:), quantile, 0);
end
t=[array2table(coeff_i), array2table(fval_i), ... % create table from the arrays
array2table(t_i), array2table(VaR), ...
array2table(coeff_j), array2table(fval_j), ...
array2table(t_j), array2table(CoVaR)];
will leave you with a table with the various variable names of the arrays as the table variable names, the coeff_x data will be a Nx3 array while the t_x are vectors. You can address any subset of rows/variables and subsets with the arrays at will.

  3 Comments

I have tried that solution, but I get the following error: "Unable to perform assignment because the size of the left side is 1-by-7 and the size of the right side is 8-by-1."
The error refers to this line of the code:
[coeff_i(j,:), fval_i(j,:), t_i(j,:), VaR(j,:), coeff_j(j,:), fval_j(j,:), t_j(j,:), CoVaR(j,:)] = ...
QR(dvar_i(1:i), dvar_j(1:i), ivar(1:i,:), quantile, 0);
Thanks in advance
Well, the 8 is a problem--where does that come from? Your table shows 16 elements for every row.
The 8x1 vs 1x8 is just transpose the RHS...but if the various variables aren't as in your sample table, then have to know what they are and somehow deal with that.
The eight returned elements would match up to there being eight LHS variables but only one element each but there are eight variables by my count on LHS.
Need more context here; what is QR? I presumed it was a function returning eight arrays each corresponding in size to the format of the pdf file layout you posed. That assumption doesn't seem to hold but there's not enough info here for us to debug with.
It is working now. The problem was with the function outputs (that need to be transposed) and with the preallocation of the outputs.
Everything is perfect now. Thank you very much for your help!

Sign in to comment.