MATLAB Answers

How can I iterate obtaining numbers?

1 view (last 30 days)
Jaime Fierro
Jaime Fierro on 2 Jul 2021
Edited: Sripranav Mannepalli on 13 Jul 2021
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 Comment
Geoff Hayes
Geoff Hayes on 2 Jul 2021
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.

Sign in to comment.

Answers (2)

Sripranav Mannepalli
Sripranav Mannepalli on 12 Jul 2021
Edited: Sripranav Mannepalli on 13 Jul 2021
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 Comment
Stephen
Stephen on 12 Jul 2021
"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:

Sign in to comment.


Stephen
Stephen on 12 Jul 2021
Edited: Stephen on 12 Jul 2021
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.

Products

Community Treasure Hunt

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

Start Hunting!