Converting string to variable name

3 vues (au cours des 30 derniers jours)
Emil Eriksson
Emil Eriksson le 13 Oct 2016
Réponse apportée : KSSV le 13 Oct 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 commentaire
Alexandra Harkai
Alexandra Harkai le 13 Oct 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

Connectez-vous pour commenter.

Réponses (1)

KSSV
KSSV le 13 Oct 2016

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by