How to store structures from a for loop

I would like to store the different structures resulting from this loop:
for i = 1:size(data,2)
[h(i),pValue(i),stat(i),cValue(i),reg(i)] = adftest(data(:,1),'model','TS','lags',1,'alpha',0.05);
regs(i) = reg(i);
end
where reg is a structure and data is 160x14 thus i = 1:14.
I obtain a 1x14 struct with 24 fields as shown below:
The code seems to store 14 structures, but they are all the same, corresponding to the first series, column 1 of 14 in data. The goal would be to store all 14 different adftest reg struct.
I attach rates.mat for replication.

1 Comment

Please do not repost the same question. If the answer doesn't work for you, explain in a comment what is missing.

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 14 Dec 2020
Edited: Star Strider on 14 Dec 2020
I had to look up that function, since I do not have the Econometrics Toolbox. (I added that to the Products tags.)
Many of those outputs are not scalars, so use cell arrays for them:
for i = 1:size(data,2)
[h{i},pValue{i},stat{i},cValue{i},reg{i}] = adftest(data(:,1),'model','TS','lags',1,'alpha',0.05);
regs{i} = reg{i};
end
Note the curly braces {} denoting cell array indexing.
See the documentation section on Access Data in Cell Array for details on how to work with cell arrays if you are not familiar with them.
EDIT — (14 Dec 2020 at 17:46)
If you have verified that all the contents are the same and you simply want to store the first result, try this:
regs{i} = reg.1;
It might be easier if I had your structure to experiment with.

7 Comments

Thank you for your quick answer. I just edited the question to add some information. I have tried your suggestion but unfortunately get the error message "Unable to perform assignment because brace indexing is not supported for variables of this type."
My pleasure!
I am not certain what you want to do with the information in that file.
I created a table from it with:
D = load('rates.mat');
data = D.data;
date = D.date;
variable = D.variable;
T1 = array2table(data, 'VariableNames',variable);
Td = table(date, 'VariableName',{'Date'});
T1 = [Td, T1];
and then:
FirstFiveRows = T1(1:5,:)
produces:
FirstFiveRows =
5×15 table
Date cas frs des its jps uks uss cal frl del itl jpl ukl usl
___________ ______ ______ ___ ___ ______ ______ ___ ___ ___ ___ ___ ___ ______ ______
01-Mar-1960 5.4633 4.9167 NaN NaN 6.4333 4.5133 NaN NaN NaN NaN NaN NaN 4.4867 3.9333
01-Jun-1960 5.19 3.98 NaN NaN 6.4667 4.8267 NaN NaN NaN NaN NaN NaN 4.26 3.6967
01-Sep-1960 4.9533 3.1067 NaN NaN 6.5667 5.5433 NaN NaN NaN NaN NaN NaN 3.8333 2.9367
01-Dec-1960 5.1467 3.9833 NaN NaN 6.2333 5.5133 NaN NaN NaN NaN NaN NaN 3.8867 2.2967
01-Mar-1961 5.1933 3.6967 NaN NaN 6 4.1733 NaN NaN NaN NaN NaN NaN 3.7867 2.0033
.
Alberto — Since you posted ‘rates.mat’ in an edit to your original Question, I deleted your Answer that initially included it. I checked first to be certain it was the same file.
The goal would be to store all 14 different adftest reg struct in order to work with the results of all 14 different reg fields.
How does that differ from the table I created from your file?
The table shows the series whether what I am looking for is to store the ADF test regression structures for each series in a table or something alike.
How does that relate to the contents of the file you posted?

Sign in to comment.

Categories

Asked:

on 14 Dec 2020

Commented:

on 16 Dec 2020

Community Treasure Hunt

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

Start Hunting!