Converting string to variable name

3 visualizaciones (últimos 30 días)
Emil Eriksson
Emil Eriksson el 13 de Oct. de 2016
Respondida: KSSV el 13 de Oct. de 2016
Hi! I'm pretty new to Matlab and have now encountered a problem. I'm trying to automate some code so that I don't have to manually edit and save figures and data. My problem lies in loading different variables in each iteration of the for-loop
Basically lets say that the variables are called x1, x2, x3,.., xn (arrays) where the for-loop goes from 1-n. So for n=1 I want to write x1 into y, and in the next loop for it to load x2 into y and so on.
Right now I'm trying to convert the n-value to a string and inserting into the a variable that is used in the code, but I can't figure out how to make matlab know I'm not creating y into a string "x1" but to figure out it's a variable it should write instead.
If anyone has a good solution for this I would greatly appriacte it!
  1 comentario
Alexandra Harkai
Alexandra Harkai el 13 de Oct. de 2016
Creating these 'indexed' variable names is generally discouraged, there are more effective ways of doing it. For example, if all x variables are the same size, you could easily make x a two-dimensional array, and in every loop you could access a row or column of it:
for j = 1:1000 y = x(j, :); end

Iniciar sesión para comentar.

Respuestas (1)

KSSV
KSSV el 13 de Oct. de 2016

Categorías

Más información sobre Characters and Strings 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