Select fields that start with specific letters in for loops

조회 수: 4 (최근 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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by