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

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 commentaires

Gabriel
Gabriel le 19 Jan 2025
Modifié(e) : Walter Roberson le 20 Jan 2025
Dear @VBBV, thank you for the guidance. It solves my first problem. however, i can't generate an IRF.
I am running MS-DSGE model using RISE toolbox. here is my code.
% Load RISE. Run this only once.
rise_startup();
%% Housekeeping
clc
clear
close all
%% Choose number of simulations and grid size:
numb_simul = 2000; % 2000
grid_size = 10; % 40
grid_size_phi = grid_size + 1; %grid_size + 1
%% Solve model
label_1={1,'low_var'; 2,'high_var'};
label_2={1,'fixed_regime_M'; 2,'ms_regimes'; 3,'ms_endog_regimes'; 4,'fixed_regime_F'};
label_3={1,'non-distortionary'; 2,'distortionary'};
m0=rise('rsa_ms',...
'rise_flags',...
struct('varmodel',label_1{1,1},...
'msmodel', label_2{2,1},...
'taxdistortionary', label_3{1,1}));
m1=solve(m0,'solve_order',2,'solve_derivatives_type','automatic','solve_shock_horizon',0);
m1.print_solution
m1.print_solution({'log_y','C','pi_ann','B_nominal','B','sp','i_ann','r_real_ann','P'})
print_solution(m0)
myirfs=irf(m0,'irf_periods',24);
mygirfs=irf(m0,'irf_type','girf','irf_periods',24,'irf_regime_specific',false);
%% plot irfs
% REGIMES IRFs
myirfs1=irf(m1,'irf_periods',24,'irf_shock_sign',1);
save('myirfs1.mat','myirfs1')
%% Auxiliar Variables
load('myirfs1.mat'); % RS
shock_type = {'eps_G','eps_nu'}; % choose 1 or 2 below
%%
myirfs1 = struct()
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 = {'eps_nu_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'};
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)
myirfs1.eps_G.var = var_aux3 % assign the data array to the struct variable
myirfs1.eps_G.var
%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)
irf_mon
irf_fisc
% Initialize the irf_mon and irf_fisc structures dynamically
irf_mon = struct();
irf_fisc = struct();
%%
load('c.mat')
TMax=25; % Maximun period on the irf charts 25.
t=1:TMax; % Auxiliar vector to X axis
var_list={'log_y','C','pi_ann','B_nominal','B','sp','i_ann','r_real_ann','P'};
Title_list={'GDP (\%)','Consumption','Inflation (\% Ann.)','Nominal Debt','Real Debt','Surplus/GDP (\%)','Nominal Interest (\% Ann.)','Real Interest (\% Ann.)','Price Level'};
figure(1);
set(1,'Color','w')
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','--');
hold on
plot(t,myirfs2.(shock_type{1}).(v).data(:,1)/10,'Color',c(10,:),'linewidth',1.5,'LineStyle',':');
title(Title_list{ii},'interpreter','latex')
hold on
plot(t,irf_mon.(v)/10,'Color',c(10,:),'linewidth',0.7,'LineStyle','-')
hold on
plot(t,irf_fisc.(v)/10,'Color',c(1,:),'linewidth',0.7,'LineStyle','-')
line([1 25], [0 0],'Color','k','LineStyle','-')
set(gca,'TickLabelInterpreter','latex') % check
if ii==1
legend({'Exogenous MS - Regime M',...
'Endogenous MS - Regime M',...
'Fixed Regime M',...
'Fixed Regime F'},'FontSize',8,'Location','northeast','interpreter','latex')
end
end
Unrecognized function or variable 'var_list'.
Unrecognized field name "log_y".
Error in tr5 (line 108)
plot(t,myirfs1.(shock_type{1}).(v).data(:,1)/10,'Color',c(10,:),'linewidth',0.7,'LineStyle','--');
I can't generate IRF. Can u suggest where to fix? Thanks
Gabriel
Gabriel le 19 Jan 2025
Modifié(e) : Walter Roberson le 20 Jan 2025
Dear @VBBV,
following the odes you suggest me, Iaccess the subfields within eps_G.
However, i can't generate an irf. could u please review the following. ps: i can take aways the C.mat data. thanks
load('c.mat')
TMax=25; % Maximun period on the irf charts. Must be consistent with IRFs periods
t=1:TMax; % Auxiliar vector to X axis
var_list={'log_y','C','pi_ann','B_nominal','B','sp','i_ann','r_real_ann','P'};
Title_list={'GDP (\%)','Consumption','Inflation (\% Ann.)','Nominal Debt','Real Debt','Surplus/GDP (\%)','Nominal Interest (\% Ann.)','Real Interest (\% Ann.)','Price Level'};
figure(1);
set(1,'Color','w')
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','--');
hold on
plot(t,myirfs2.(shock_type{1}).(v).data(:,1)/10,'Color',c(10,:),'linewidth',1.5,'LineStyle',':');
title(Title_list{ii},'interpreter','latex')
hold on
plot(t,irf_mon.(v)/10,'Color',c(10,:),'linewidth',0.7,'LineStyle','-')
hold on
plot(t,irf_fisc.(v)/10,'Color',c(1,:),'linewidth',0.7,'LineStyle','-')
line([1 25], [0 0],'Color','k','LineStyle','-')
set(gca,'TickLabelInterpreter','latex') % check
if ii==1
legend({'Exogenous MS - Regime M',...
'Endogenous MS - Regime M',...
'Fixed Regime M',...
'Fixed Regime F'},'FontSize',8,'Location','northeast','interpreter','latex')
end
end
VBBV
VBBV le 20 Jan 2025
Modifié(e) : VBBV le 20 Jan 2025
@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','--')
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]

Connectez-vous pour commenter.

 Réponse acceptée

VBBV
VBBV le 19 Jan 2025
Déplacé(e) : VBBV le 20 Jan 2025
@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]}

Plus de réponses (1)

Gabriel
Gabriel le 21 Jan 2025
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','--')

Catégories

En savoir plus sur Develop Apps Using App Designer dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by