Converting string to variable name

3 次查看(过去 30 天)
Emil Eriksson
Emil Eriksson 2016-10-13
回答: KSSV 2016-10-13
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 个评论
Alexandra Harkai
Alexandra Harkai 2016-10-13
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

请先登录,再进行评论。

回答(1 个)

KSSV
KSSV 2016-10-13

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by