Converting string to variable name

3 visualizzazioni (ultimi 30 giorni)
Emil Eriksson
Emil Eriksson il 13 Ott 2016
Risposto: KSSV il 13 Ott 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 Commento
Alexandra Harkai
Alexandra Harkai il 13 Ott 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

Accedi per commentare.

Risposte (1)

KSSV
KSSV il 13 Ott 2016

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by