Is there way to easily change the value of multiple values using a for loop?
12 views (last 30 days)
This is hard for me to explain, but I was wondering if there was a way to use a for loop to simlify this code. My problem is that I dont know how I could call the specific variable in the for loop that is needed to be changed.
k1_exact = -1;
k2_exact = -2;
k1_U = -3;
k1_L = -4;
k2_U = -5;
k2_L = -6;
Sahil Jain on 17 Nov 2021
Hi Nick. You can try using the "eval" function for your purpose. The following code snippet demonstrates how you may go about it.
variables = ["k1_exact", "k2_exact", "k1_U", "k1_L", "k2_U", "k2_L"];
eval(strcat(variables(i), "=-", num2str(i)))
For every variable, we first create a string of the assignment expression. We then pass this string to the "eval" function to execute the operation.