# Modifying elements of fields in structures

5 views (last 30 days)
alphabetagamma on 12 Aug 2022
Commented: alphabetagamma on 16 Aug 2022
This is similar to Select fields that start with specific letters in for loops - (mathworks.com), but I thought of making a separate post as I would like ask two questions.
I have a humongus structure known as
big_struct
with several fields. Each of these fields are doubles (30 x 1) dimension. So, they contain 30 numerical values.
Examples of these doubles are price_SH_RES_output_SH, price_AM_RES_output_SH etc.
I have two questions, but the second question is more important.
Q1: The following code becomes cumbersome when we add multiple fields, and I was wondering how to make it efficient and succint. If you could show the modifed code (perhaps by using a for loop?), then that would be very helpful.
interested_variable_vec = ["price", "output", "bond"];
interested_town_vec = ["South Hadley", "Amherst", "State College"];
town_code = ["SH", "AM", "SC"];
choose_variable1 = "price_SH";
choose_variable2 = "output_SH";
choose_variable3 = "bond_SH";
[~,town_idx] = ismember(town_of_interest, town_vec);
if town_idx == 0
error('unknown town of interest: "%s"', town_of_interest);
end
if ~strcmp(interested_variable, 'bond')
fieldname1 = interested_variable + "_" + town_code(town_idx) +"_RES_" + choose_variable1;
fieldname2 = interested_variable + "_" + town_code(town_idx) +"_RES_" + choose_variable2;
fieldname3 = interested_variable + "_" + town_code(town_idx) +"_RES_" + choose_variable3;
end
if strcmp(interested_variable, 'bond')
if ~strcmp(town_code, 'SH')
fieldname1 = interested_variable + "_" + town_code(town_idx) + "_RES_" + choose_variable1;
fieldname2 = interested_variable + "_" + town_code(town_idx) + "_RES_" + choose_variable2;
fieldname3 = interested_variable + "_" + town_code(town_idx) + "_RES_" + choose_variable2;
end
end
if ~isfield(big_struct, fieldname1)
error('town and variable combination does not exist: "%s"', fieldname1);
end
if ~isfield(big_struct, fieldname2)
error('town and variable combination does not exist: "%s"', fieldname1);
end
impulse1 = big_struct.(fieldname1);
impulse2 = big_struct.(fieldname2);
impulse3 = big_struct.(fieldname3);
Q2. How do we modify the elements of a field? For instance, impulse1 is a (30 x 1). I would like the first 3 elements of impulse1, impulse2, impulse3 to take the first value in these respective fields
Example: if the elements of impulse1 are:
1.21
1.01
0.95
0.85
0.78
0.66
...
0.01
Then, I would like to the elements to be modified to:
1.21
1.21
1.21
1.01
0.95
0.85
...
...
0.01
I would like to do the same for the other fields and use them them below
% Add the projections of the variables with the forecast intervals
% create a (23 x 1) dimension of empty array with 0s
% length(impulse) = 20 as the horizon is 20 quarters into the future
difference = zeros(4 + length(impulse1) - 1, 1);
for ii = 1:4
% impulse prints the values from IRFs = (20 x 1) array
difference(ii:horizon+ii-1) = difference(ii:horizon+ii-1) + 3*(impulse1 .* impulse2 .* impulse3);
end
Thank you so much for your help in advance.
alphabetagamma on 16 Aug 2022
Haha thanks for the grade, and for helping me understand solve this problem.