Changing part of a name of a variable in a for loop

In summary, the conversation discusses using a "for" loop in MATLAB to calculate equations for different time points and how to change a part of a variable name within the loop. The suggested solution is to use an array or the eval function with genvarname.
  • #1
loukoumas
15
0
changing part of a name of a variable in a "for" loop

hello everyone!

let's say i have the equation between two variables: DT10sec= A*T10sec, working in matlab. How can i use a "for" loop to calculate the equation DT20sec= A*T20sec DT30sec= A*T30sec, DT40sec= A*T40sec,... etc

How can i interfere in just a part of the name of a variable? i would suppose something like the following

for i=1:5
DT"i"sec= A* T"i"sec
end

but it is not the right symbol
 
Physics news on Phys.org
  • #2


I would use an array. I don't know the MATLAB syntax, but most programming languages would write this as DTsec[10] or (with a variable i) DTsec[10*i].
 
  • #3


mfb said:
I would use an array. I don't know the MATLAB syntax, but most programming languages would write this as DTsec[10] or (with a variable i) DTsec[10*i].
DT..sec and T..sec are matrices and i m not interested on making an array of matrices. DT is the result from calculations (as T is a known matrix) for different time points and i just want to name my results according to the time point they refer to. But i am looking for a quicker way to do it, using a for loop. Thanks anyway
 
  • #5
.

I would first clarify the goal and purpose of changing the variable name in the for loop. Is it for organizational purposes, to make the code more efficient, or for a specific scientific reason? Once the purpose is clear, I would recommend using a different approach to achieve the desired result.

For example, if the goal is to make the code more efficient, I would suggest using arrays or matrices to store the values of DT and T, and then use a for loop to iterate through the values and calculate the equation.

If the purpose is to change the variable names for organizational purposes, I would recommend using a naming convention that is easily understandable and clear to other scientists who may read or use the code. This could include using descriptive variable names and comments to explain the purpose of each variable.

Interfering with just a part of a variable name can also lead to confusion and errors in the code. It is important to ensure that the changes made to the variable names are clear and do not affect the functionality of the code.

In summary, as a scientist, I would suggest carefully considering the purpose and potential impacts of changing part of a variable name in a for loop and using alternative approaches to achieve the desired result.
 

Related to Changing part of a name of a variable in a for loop

1. How do I change part of a variable name in a for loop?

In order to change part of a variable name in a for loop, you can use string concatenation or string interpolation to dynamically create a new variable name with the desired changes.

2. Can I use regular expressions to change part of a variable name in a for loop?

Yes, you can use regular expressions to find and replace specific patterns within a string, including variable names. This can be useful for making multiple changes to variable names within a for loop.

3. Is it possible to change a variable name in a for loop without creating a new variable?

No, in order to change a variable name in a for loop, you will need to create a new variable with the desired changes. This is because variable names cannot be changed once they have been declared.

4. Can I change the name of an existing variable within a for loop?

No, you cannot change the name of an existing variable within a for loop. However, you can assign a new value to the existing variable which effectively changes its value.

5. Are there any specific rules or limitations for changing part of a variable name in a for loop?

Yes, there are some limitations when it comes to changing part of a variable name in a for loop. For example, variable names cannot contain spaces or special characters, and they must start with a letter or underscore.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • Beyond the Standard Models
Replies
7
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • Programming and Computer Science
Replies
16
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
2K
  • Electrical Engineering
2
Replies
44
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • Calculus and Beyond Homework Help
Replies
5
Views
923
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
Back
Top