How can I iterate obtaining numbers?

조회 수: 2 (최근 30일)
Jaime Fierro
Jaime Fierro 2021년 7월 2일
편집: Sripranav Mannepalli 2021년 7월 13일
Hello!
I am trying to loop the following code:
for i = ["US", "DE", "JP"]
P.std_E_LY_i_HAT = 2
P.std_E_LY_i_BAR = 0.3 * 0.7 P.std_E_LY_i_HAT
end
The idea is to change the i in the code for the strings in the vector. What I am expecting to get is:
P.std_E_LY_US_HAT = 2
P.std_E_LY_US_BAR = 0.3 * 0.7 P.std_E_LY_US_HAT
P.std_E_LY_DE_HAT = 2
P.std_E_LY_DE_BAR = 0.3 * 0.7 P.std_E_LY_DE_HAT
P.std_E_LY_JP_HAT = 2
P.std_E_LY_JP_BAR = 0.3 * 0.7 P.std_E_LY_JP_HAT
Where P.std_E_LY_ is notation that is used in Iris, a time series aplication for MatLab.
I tried this way
for i = ["US", "DE", "JP"]
strcat('P.std_E_LY_', i, '_HAT = 1')
strcat('P.std_E_LY_', i, '_BAR = 0.3 * 0.7 P.std_E_LY_', i, '_HAT')
end
My problem is that the as output of the loop I get strings but I do not get 1, 0.3 and 0.7 as numbers.
Can anyone please help me out with this?
  댓글 수: 1
Geoff Hayes
Geoff Hayes 2021년 7월 2일
Jamie - why not just use an array to store your data rather than dynamically creating field names for the struct? When you need to write the output to file or the console, then you could name with the appropriate US, DE, or JP value.

댓글을 달려면 로그인하십시오.

답변 (2개)

Sripranav Mannepalli
Sripranav Mannepalli 2021년 7월 12일
편집: Sripranav Mannepalli 2021년 7월 13일
Hi,
It’s my understanding that you are trying to replace 'i' in P.std_E_LY_i_HAT and P.std_E_LY_i_BAR with US, DE, JP respectively in each iteration.
Hence, the expected in the first iteration are P.std_E_LY_US_HAT and P.std_E_LY_US_BAR, in the second iteration are P.std_E_LY_DE_HAT and P.std_E_LY_DE_BAR and in the third iteration are P.std_E_LY_JP_HAT and P.std_E_LY_JP_BAR.
One possible way to do this is :
for i = ["US", "DE", "JP"]
if (i == "US")
P.std_E_LY_US_HAT = 2 ;
P.std_E_LY_US_BAR = 0.3 * 0.7 * P.std_E_LY_US_HAT; % assuming 0.7 P.std_E_LY_US_HAT means multipying 0.7 and P.std_E_LY_US_HAT
elseif (i == "DE")
P.std_E_LY_DE_HAT = 2 ;
P.std_E_LY_DE_BAR = 0.3 * 0.7 * P.std_E_LY_DE_HAT; % assuming 0.7 P.std_E_LY_DE_HAT means multipying 0.7 and P.std_E_LY_DE_HAT
elseif (i == "JP")
P.std_E_LY_JP_HAT = 2 ;
P.std_E_LY_JP_BAR = 0.3 * 0.7 * P.std_E_LY_JP_HAT; % assuming 0.7 P.std_E_LY_JP_HAT means multipying 0.7 and P.std_E_LY_JP_HAT
end
end
  댓글 수: 1
Stephen23
Stephen23 2021년 7월 12일
"It’s my understanding that you are trying to replace 'i' in the variables ..."
In MATLAB terminology the fields of the structure have fieldnames. The question actually asks how to change fieldnames, not variable names. Learn more about MATLAB structures and their fieldnames:

댓글을 달려면 로그인하십시오.


Stephen23
Stephen23 2021년 7월 12일
편집: Stephen23 2021년 7월 12일
You can use dynamic fieldnames:
for kk = ["US","DE","JP"]
P.("std_E_LY_"+kk+"_HAT") = 2;
P.("std_E_LY_"+kk+"_BAR") = 0.3 * 0.7 * P.("std_E_LY_"+kk+"_HAT");
end
P
P = struct with fields:
std_E_LY_US_HAT: 2 std_E_LY_US_BAR: 0.4200 std_E_LY_DE_HAT: 2 std_E_LY_DE_BAR: 0.4200 std_E_LY_JP_HAT: 2 std_E_LY_JP_BAR: 0.4200
But this would likely be quite an inefficient way to store/process this data.
Most likely you should be using arrays, just like MATLAB was designed for.

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by