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

  • Thread starter Thread starter loukoumas
  • Start date Start date
  • Tags Tags
    Loop Variable
AI Thread Summary
To calculate multiple variables in MATLAB using a "for" loop, the discussion centers on the challenge of dynamically naming variables like DT10sec, DT20sec, etc. The proposed solution involves using a loop to generate these variable names, but the correct approach in MATLAB is to utilize arrays instead of trying to create variable names dynamically. The suggestion to use the `eval` function alongside `genvarname` is mentioned, but it is generally advised to avoid `eval` due to potential issues with code readability and performance. Instead, using indexed arrays for storing results is recommended, as it simplifies the process and adheres to MATLAB's syntax for handling matrices. This method allows for efficient calculations without the complications of dynamically named variables.
loukoumas
Messages
14
Reaction score
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


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].
 


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
 

Similar threads

Replies
4
Views
2K
Replies
4
Views
1K
Replies
28
Views
731
Replies
16
Views
3K
Replies
6
Views
4K
Replies
7
Views
3K
Replies
4
Views
2K
Replies
1
Views
3K
Replies
5
Views
1K
Back
Top