Select fields that start with specific letters in for loops

1 view (last 30 days)
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 am trying to do the following, but it is very inefficient and time consuming.
interested_variable_vec = ['price', 'output', 'bond']
interested_town_vec = ['South Hadley', 'Amherst', 'State College']
% interested_variable takes any one of the variable from
% interested_variable_variable_vec. Eg: interested_variable = 'price'
if strcmp(interested_variable, 'price') && strcmp(interested_town, 'South Hadley')
innovation = big_struct.price_SH_RES_output_SH;
elseif strcmp(interested_variable, 'price') && strcmp(interested_town, 'Amherst')
innovation = big_struct.price_AM_RES_output_SH;
elseif strcmp(interested_variable, 'price') && strcmp(interested_town, 'State College')
innovation = big_struct.price_EU_RES_output_SH;
elseif strcmp(interested_variable, 'output') && strcmp(interested_town, 'South Hadley')
innovation = big_struct.output_SH_RES_output_SH;
elseif strcmp(interested_variable, 'output') && strcmp(interested_town, 'Amherst')
innovation = big_struct.output_AM_RES_output_SH;
elseif strcmp(interested_variable, 'bond') && strcmp(interested_town, 'South Hadley')
innovation = big_struct.bond_SH_RES_output_SH;
elseif strcmp(interested_variable, 'bond') && strcmp(interested_town, 'Amherst')
innovation = big_struct.bond_AM_RES_output_SH;
elseif strcmp(interested_variable, 'bond') && strcmp(interested_town, 'State College')
innovation = big_struct.bond_EU_RES_output_SH;
end
I thought of using a for loop, but I am not good at for loops. Here's what I think the for loop might look like, but I am not sure how to implement it. I appreciate if you could help fix it.
shock = output_SH;
for i = length(city_vec)
for j = 1:length(interested_variable_vec)
if strcmp(interested_variable, interested_variable_vec(j,:)) && strcmp(interested_town, interested_town_vec(i,:))
innovation = big_struct.j_i_RES_shock;
end
end

Accepted Answer

Walter Roberson
Walter Roberson on 23 Jul 2022
Edited: Walter Roberson on 23 Jul 2022
%do not use apostrophes, use double-quote for this work
interested_variable_vec = ["price", "output", "bond"];
interested_town_vec = ["South Hadley", "Amherst", "State College"];
town_code = ["SH", "AM", "EU"];
if ~ismember(interested_variable, interested_variable_vec)
error('unknown interested variable: "%s"', interested_variable);
end
[~,town_idx] = ismember(intereted_town, interested_town_vec);
if town_idx == 0
error('unknown interested town: "%s"', interested_town);
end
fieldname = interested_variable + "_" + town_code(town_idx) + "_RES_output_SH";
if ~isfield(big_struct, fieldname)
error('variable and town combination does not exist: "%s"', fieldname);
end
innovation = big_struct.(fieldname);
  2 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Data Preprocessing in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!