For Loop with strings

2 visualizaciones (últimos 30 días)
Edoardo Briganti
Edoardo Briganti el 27 de Jun. de 2017
Respondida: Pratik Anand el 17 de Jul. de 2017
I am dealing with n vectors which represent the values of a variable along time. Each of them is characterized by a code which can be both numerical and text. For example Y_23 and Y_FIRE. I have to make some computations for each of them, for instance: beta_23 = Y_23^2. I was thinking about indexing the variables or making a for loop with string vectors. Anybody may help me please? Thanks in advance.
  2 comentarios
Stephen23
Stephen23 el 27 de Jun. de 2017
"I was thinking about indexing the variables"
That is a good idea. You should do that.
"or making a for loop with string vectors"
That is most likely a bad idea, because accessing variables from strings is slow, buggy, hard to debug, hard to read... and is strongly discouraged:
Although indexing is likely the best option, another option would be for you to put all of your data into a structure, then you can simply access and pass any of those values by using the fieldnames.
Edoardo Briganti
Edoardo Briganti el 27 de Jun. de 2017
Thanks for your help Stephen. I will follow your advice.
Have a nice day.

Iniciar sesión para comentar.

Respuestas (1)

Pratik Anand
Pratik Anand el 17 de Jul. de 2017
Adding Stephen Cobeldick 's suggestion here so that it is visible as an answer rather than a comment.
"I was thinking about indexing the variables"
That is a good idea. You should do that.
"or making a for loop with string vectors"
That is most likely a bad idea, because accessing variables from strings is slow, buggy, hard to debug, hard to read... and is strongly discouraged:
Although indexing is likely the best option, another option would be for you to put all of your data into a structure, then you can simply access and pass any of those values by using the fieldnames.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by