How can I iterate obtaining numbers?

1 次查看(过去 30 天)
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-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