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
Click For Summary

Discussion Overview

The discussion revolves around how to dynamically change part of a variable name within a "for" loop in MATLAB, specifically for calculating values based on a given equation involving multiple time points. Participants explore different approaches to achieve this functionality.

Discussion Character

  • Technical explanation, Debate/contested, Conceptual clarification

Main Points Raised

  • One participant seeks a method to calculate DT values for different time points using a "for" loop, expressing difficulty in modifying variable names dynamically.
  • Another participant suggests using an array as a solution, referencing common programming syntax, but does not provide MATLAB-specific guidance.
  • A third participant reiterates the array suggestion but clarifies that they are not interested in creating an array of matrices, emphasizing the need for naming results according to specific time points.
  • A later reply proposes using the `eval` function along with `genvarname` to create variable names dynamically, providing a link to MATLAB documentation for further reference.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach, with differing opinions on the use of arrays versus dynamic variable naming through `eval`.

Contextual Notes

The discussion highlights limitations in MATLAB's handling of variable names and the potential complexity of using `eval`, which may introduce risks or inefficiencies in code execution.

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 ·
Replies
4
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 28 ·
Replies
28
Views
2K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 2 ·
Replies
2
Views
956
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K