How to store structures from a for loop
Mostrar comentarios más antiguos
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 comentario
Rik
el 15 de Dic. de 2020
Please do not repost the same question. If the answer doesn't work for you, explain in a comment what is missing.
Respuestas (1)
Star Strider
el 14 de Dic. de 2020
Editada: Star Strider
el 14 de Dic. de 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 comentarios
Alberto
el 14 de Dic. de 2020
Star Strider
el 14 de Dic. de 2020
My pleasure!
I am not certain what you want to do with the information in that file.
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
.
Star Strider
el 14 de Dic. de 2020
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.
Alberto
el 15 de Dic. de 2020
Star Strider
el 15 de Dic. de 2020
How does that differ from the table I created from your file?
Alberto
el 16 de Dic. de 2020
Star Strider
el 16 de Dic. de 2020
How does that relate to the contents of the file you posted?
Categorías
Más información sobre Cell Arrays en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!