Select fields that start with specific letters in for loops

3 ビュー (過去 30 日間)
alphabetagamma
alphabetagamma 2022 年 7 月 23 日
コメント済み: Walter Roberson 2022 年 7 月 23 日
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

採用された回答

Walter Roberson
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 件のコメント
alphabetagamma
alphabetagamma 2022 年 7 月 23 日
編集済み: alphabetagamma 2022 年 7 月 23 日
This is great. Thanks a lot. However, town_code(town_idx) seems to output only the first letter.
e.g.:
town_code(town_idx)
output is S, instead of SH probably because the index is 1.
Consequently, the fieldname price_S_RES_output_SH doesn't exist and yields an error. Is there any way to fix that?
Walter Roberson
Walter Roberson 2022 年 7 月 23 日
Read the very first line of the code, the comment.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by