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