Select fields that start with specific letters in for loops
1 回表示 (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
Walter Roberson
2022 年 7 月 23 日
編集済み: Walter Roberson
2022 年 7 月 23 日
%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 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Structures についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!