- https://www.mathworks.com/help/matlab/cell-arrays.html
- https://www.mathworks.com/help/matlab/structures.html
What is the correct syntax to assign simulated data to my variables?
71 ビュー (過去 30 日間)
古いコメントを表示
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?
0 件のコメント
回答 (2 件)
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.
0 件のコメント
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.eps_G
C = G_var_list;
C(end+1,:) = {[]};
myirfs1.eps_G = struct(C{:});
myirfs1.eps_G
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})
irf_fisc
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で String Parsing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!