String to variable value
Ältere Kommentare anzeigen
Hello,
I am using Matlab for a college aplicattion where I have to read and operate about 200 vectors. My problem is that I do not know any way of coding able to do this authomatically, I mean, I wouldn't like to write the whole vectors one by one.
The way of operating is the next: I have about 100 electric voltage vectors with their respective values in time, and, in the same way, another 100 current vectors. What I want to do is get them divided one by one and put the results into another vector which shows electric resistance. Obiously, this division must be point by point.
Every vector is in the workspace and I made a little programm that doesn't seem to work at all. I have named them using a suffix so that I know which vector is everyone (V1, V2, V3 ... V100 and I1, I2, I3 ... I100).
In my code I use the function "strcat" to link my "V" and "I" vector with their respective number that I take from a for loop and then operate them, but the problem comes when I try to access these variables because they are only string names. How could I get the variable values from those strings?
Thank you a lot
Nicolás González
6 Kommentare
Adam Danz
am 17 Jun. 2019
"The way of operating is the next: I have about 100 electric voltage vectors with their respective values in time, and, in the same way, another 100 current vectors. What I want to do is get them divided one by one and put the results into another vector which shows electric resistance. "
If each vector is the same length, I would put all 100 voltage vectors into a matrix and all 100 current vectors into a matrix. Then you can divide the two matrices element-wise which will give you a 3rd matrix "M" where M(i,j) is the resistence of voltage(i,j) and current(i,j).
Nicolás González
am 17 Jun. 2019
Stephen23
am 17 Jun. 2019
"I have named them using a suffix so that I know which vector is everyone (V1, V2, V3 ... V100 and I1, I2, I3 ... I100)."
Do NOT do this!
Numbering variables is a sign that you are doing something wrong in your code design.
Dynamically accessing variable names is one way that beginners force themselves into writing slow, complex, obfuscated, buggy code that is hard to debug. Read this to know why:
You should use indexing (e.g. in one matrix as Adam Danz suggested). Indexing is neat, simple, easy to debug, and very efficient (unlike what you are trying to do).
Nicolás González
am 17 Jun. 2019
Adam Danz
am 17 Jun. 2019
If you get stuck, you can update us where you're at here in the comment section.
Nicolás González
am 17 Jun. 2019
Antworten (0)
Kategorien
Mehr zu Matrix Indexing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!