Select fields that start with specific letters in for loops
Ältere Kommentare anzeigen
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
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Variables finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!