What is the correct syntax to assign simulated data to my variables?

71 ビュー (過去 30 日間)
Gabriel
Gabriel 2025 年 1 月 22 日 10:51
回答済み: Voss 2025 年 2 月 2 日 0:33
Dears,
I am running a MS-DSGE model using RISE toolbox. I want to add a fiscal shock and examine its effect on output, price...
%fiscal shock
shock_type = {'eps_G'};
%here is my variable list of a cell array of character variables and not a struct.
var_list={'log_y','C','pi_ann','B_nominal','B','sp','i_ann','r_real_ann','P'};
% EXOGENOUS SWITCHING
myirfs1=irf(m1,'irf_periods',24,'irf_shock_sign',1);
% following the suggestion by @VBBV, I use the following sintaxes to access elements of struct
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_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
irf_fisc = struct();
for i = 1:numel(var_aux)
irf_fisc.var_aux{i} = [0,myirfs1.eps_G.var{i}]';
end
irf_fisc.var_aux(1)
irf_fisc
% what is the write syntax to assign value (simulated data) to the struct?
myirfs1.eps_G.logy = data(:,1)/10;
%Is the suggested code. but where is the data variable located? should I create it
%data = randn(TMax, N); or it is already simulated?

回答 (2 件)

Epsilon
Epsilon 2025 年 1 月 31 日 12:37
Hi Gabriel,
In the provided code the variable ‘myirfs1’ is a structure array which is assigned different struct fields by using the syntax ‘myirfs1.eps_CP = struct();’. The variable ‘var_aux3’ is a cell array with the 9 set of cells containing strings.
Please note that the values in the cell array, eg: ‘log_y’,’C’,’pi_ann’,etc. are strings and not variables. Therefore, they cannot store any value instead the cell can be replaced by a new cell containing a different value.
It seems there is some confusion between the usage/syntax of cell arrays and structures. Please follow the link to the documentation on cell arrays and structures:
The documentation contains relevant links to assigning and accessing data in these datatypes.

Voss
Voss 2025 年 2 月 2 日 0:33
This may be a useful reference:
G_var_list = {'log_y','C','pi_ann','B_nominal','B','sp','i_ann','r_real_ann','P'};
irfs_var_list = strcat('eps_',{'CP','G','T','a','nu','z'});
C = irfs_var_list;
C(end+1,:) = {struct()};
myirfs1 = struct(C{:});
myirfs1
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
ans = struct with no fields.
C = G_var_list;
C(end+1,:) = {[]};
myirfs1.eps_G = struct(C{:});
myirfs1.eps_G
ans = struct with fields:
log_y: [] C: [] pi_ann: [] B_nominal: [] B: [] sp: [] i_ann: [] r_real_ann: [] P: []
irf_fisc = struct();
for i = 1:numel(G_var_list)
irf_fisc.(G_var_list{i}) = [0,myirfs1.eps_G.(G_var_list{i})].';
end
irf_fisc.(G_var_list{1})
ans = 0
irf_fisc
irf_fisc = struct with fields:
log_y: 0 C: 0 pi_ann: 0 B_nominal: 0 B: 0 sp: 0 i_ann: 0 r_real_ann: 0 P: 0

カテゴリ

Help Center および File ExchangeString Parsing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by