how can i create (generate) the Loop to Access Subfields:

1 回表示 (過去 30 日間)
Gabriel
Gabriel 2025 年 1 月 19 日
回答済み: Gabriel 2025 年 1 月 21 日
Dears,
I need your help. hocan I access the subfields within eps_G, where eps_G is a structure.
whos myirfs1
Name Size Bytes Class Attributes
myirfs1 1x1 374094 struct
%% disp(fieldnames(myirfs1))
>> disp(fieldnames(myirfs1))
{'eps_CP'}
{'eps_G' }
{'eps_T' }
{'eps_a' }
{'eps_nu'}
{'eps_z' }
% choose 1 or 2 below
shock_type = {'eps_G','eps_nu'};
var_aux = {'log_y','C','pi_ann','B_nominal','B','sp','i_ann','r_real_ann','P'};
var_aux2 = {'log_y_eps_nu','C_eps_nu','pi_ann_eps_nu','B_nominal_eps_nu','B_eps_nu','sp_eps_nu','i_ann_eps_nu','r_real_ann_eps_nu','P_eps_nu'};
var_aux3 = {'eps_G_log_y','eps_G_C','eps_G_pi_ann','eps_G_B_nominal','eps_G_B','eps_G_sp','eps_G_i_ann','eps_G_r_real_ann','eps_G_P'};
%Irfs of monetary and fiscal policy
irf_mon = struct();
irf_fisc = struct();
%% disp(fieldnames(myirfs1))
>> disp(fieldnames(myirfs1))
{'eps_CP'}
{'eps_G' }
{'eps_T' }
{'eps_a' }
{'eps_nu'}
{'eps_z' }
% when i run the following code it is unrecognized. can you suggest me what to do?
for i = 1:numel(var_aux)
irf_mon.(var_aux{i}) = [0,myirfs1(1).(var_aux3{i})]';
irf_fisc.(var_aux{i}) = [0,myirfs1(1).(var_aux3{i})]';
end
Unrecognized field name "log_y_eps_G".
  4 件のコメント
VBBV
VBBV 2025 年 1 月 20 日
編集済み: VBBV 2025 年 1 月 20 日
@Gabriel why do you reinitialize the irf_mon and ir_fisc structures ? They become empty again when you reintialize and erases all previous data.
% Initialize the irf_mon and irf_fisc structures dynamically
%irf_mon = struct(); comment these lines
%irf_fisc = struct(); comment these lines
%%
load('c.mat')
for ii=1:numel(var_list)
subplot(3,3,ii)
v=var_list{ii};
plot(t,myirfs1.(shock_type{1}).(v).data(:,1)/10,'Color',c(10,:),'linewidth',0.7,'LineStyle','--');
The above syntax for structure inside the plot function is not correct. What is data(:,1)/10 ? where is it located ? probably inside c.mat file? Note that var_list is cell array of character variables and not a struct. You need to assign data first to that variable. The correct syntax is
myirfs1.eps_G.logy = data(:,1)/10;
plot(t,myirfs1.eps_G.logy,'Color',c(10,:),'linewidth',0.7,'LineStyle','--')
Gabriel
Gabriel 2025 年 1 月 20 日
Dear @VBBV, thank for fixing my code.
however,
myirfs1.eps_G.logy= data(:,1)/10; %is not working. I thought the 'logy' is 'log_y' but still dind't work.
I tried with both variable names.
myirfs1.eps_G.logy= data(:,1)/10;
Unrecognized function or variable 'data'.
in all cases, the 'data' is unrecognized. May you please, check it again.
% after putting only the codes u suggest me, I got a bit differnt in the following.
irf_mon =
struct with fields:
var_aux: {1×9 cell}
>> irf_fisc
irf_fisc =
struct with fields:
var_aux: {1×9 cell}
%other surpsing thing. why is myirfs1.eps_G is changed to var: {1×9 cell} while others are [25×1 ts].
myirfs=irf(m0,'irf_periods',24,'irf_shock_sign',1);
save('myirfs.mat','myirfs')
myirfs1=irf(m1,'irf_periods',24,'irf_shock_sign',1);
save('myirfs1.mat','myirfs1')
myirfs2=irf(m0,'irf_type','girf','irf_periods',24,'irf_regime_specific',false);
save('myirfs2.mat','myirfs2')
myirfs3=irf(m1,'irf_periods',24,'irf_shock_sign',0.0025,'simul_pruned',true,'irf_draws',1000);
save('myirfs3.mat','myirfs3')
%%% one further question. why is myirfs1.eps_G is changed to var: {1×9 cell} while others are [25×1 ts]. is it because I conduct: myirfs1 = struct()
>> disp(myirfs1.eps_G);
var: {1×9 cell}
>> disp(myirfs2.eps_G);
A: [25×1 ts]
B: [25×1 ts]
B_nominal: [25×1 ts]
C: [25×1 ts]
CE: [25×1 ts]
G: [25×1 ts]
MC: [25×1 ts]
N: [25×1 ts]
N_nat: [25×1 ts]
P: [25×1 ts]
Pi: [25×1 ts]
Pi_star: [25×1 ts]
Q: [25×1 ts]
R: [25×1 ts]
R_real: [25×1 ts]
S: [25×1 ts]
SP: [25×1 ts]
T: [25×1 ts]
Utility: [25×1 ts]
W_real: [25×1 ts]
Welfare: [25×1 ts]
Y: [25×1 ts]
Y_gap: [25×1 ts]
Y_nat: [25×1 ts]
Z: [25×1 ts]
b: [25×1 ts]
epsilon: [25×1 ts]
i_ann: [25×1 ts]
inflation: [25×1 ts]
interest_rate: [25×1 ts]
log_A: [25×1 ts]
log_N: [25×1 ts]
log_P: [25×1 ts]
log_W_real: [25×1 ts]
log_Z: [25×1 ts]
log_y: [25×1 ts]
nu: [25×1 ts]
output_gap: [25×1 ts]
pi_ann: [25×1 ts]
r_real_ann: [25×1 ts]
sp: [25×1 ts]
x_aux_1: [25×1 ts]
x_aux_2: [25×1 ts]

サインインしてコメントする。

採用された回答

VBBV
VBBV 2025 年 1 月 19 日
移動済み: VBBV 2025 年 1 月 20 日
@Gabriel To access elements of struct which are themselves struct,
you need to use the . operator to next level of structure
myirfs1 = struct()
myirfs1 = struct with no fields.
myirfs1.eps_CP = struct();
myirfs1.eps_G = struct();
myirfs1.eps_T = struct();
myirfs1.eps_a = struct();
myirfs1.eps_nu = struct();
myirfs1.eps_z = struct();
var_aux = {'log_y','C','pi_ann','B_nominal','B','sp','i_ann','r_real_ann','P'};
var_aux2 = {'log_y_eps_nu','C_eps_nu','pi_ann_eps_nu','B_nominal_eps_nu','B_eps_nu','sp_eps_nu','i_ann_eps_nu','r_real_ann_eps_nu','P_eps_nu'};
var_aux3 = {'eps_G_log_y','eps_G_C','eps_G_pi_ann','eps_G_B_nominal','eps_G_B','eps_G_sp','eps_G_i_ann','eps_G_r_real_ann','eps_G_P'};
fieldnames(myirfs1)
ans = 6x1 cell array
{'eps_CP'} {'eps_G' } {'eps_T' } {'eps_a' } {'eps_nu'} {'eps_z' }
myirfs1.eps_G.var = var_aux3 % assign the data array to the struct variable
myirfs1 = struct with fields:
eps_CP: [1x1 struct] eps_G: [1x1 struct] eps_T: [1x1 struct] eps_a: [1x1 struct] eps_nu: [1x1 struct] eps_z: [1x1 struct]
myirfs1.eps_G.var
ans = 1x9 cell array
{'eps_G_log_y'} {'eps_G_C'} {'eps_G_pi_ann'} {'eps_G_B_nominal'} {'eps_G_B'} {'eps_G_sp'} {'eps_G_i_ann'} {'eps_G_r_real_ann'} {'eps_G_P'}
%Irfs of monetary and fiscal policy
irf_mon = struct();
irf_fisc = struct();
for i = 1:numel(var_aux)
irf_mon.var_aux{i} = [0,myirfs1.eps_G.var{i}]';
irf_fisc.var_aux{i} = [0,myirfs1.eps_G.var{i}]';
end
irf_mon.var_aux(1)
ans = 1x1 cell array
{12x1 char}
irf_mon
irf_mon = struct with fields:
var_aux: {[12x1 char] [8x1 char] [13x1 char] [16x1 char] [8x1 char] [9x1 char] [12x1 char] [17x1 char] [8x1 char]}
irf_fisc
irf_fisc = struct with fields:
var_aux: {[12x1 char] [8x1 char] [13x1 char] [16x1 char] [8x1 char] [9x1 char] [12x1 char] [17x1 char] [8x1 char]}

その他の回答 (1 件)

Gabriel
Gabriel 2025 年 1 月 21 日
Dear @VBBV, following the script you suggested me, I can generate IRFs but they are insignificant. I thought, it might be because of the 'data'.
since, I am simulating the model, I assign data in this way. Is it okay/ correct? what do you suggest me how to assign the data? attached the IRFs simulated
TMax = 25; % Maximum period on the IRF charts
N = 9; % Number of variables to simulate
t = 1:TMax;
c = lines(10); % Example color matrix with 10 colors
data = randn(TMax, N); % TMax x N matrix of random values
myirfs1.eps_G.logy = data(:,1)/10;
plot(t,myirfs1.eps_G.logy,'Color',c(10,:),'linewidth',0.7,'LineStyle','--')

カテゴリ

Help Center および File ExchangeDisplay and Presentation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by