convert strings back to variable names?
    조회 수: 1 (최근 30일)
  
       이전 댓글 표시
    
I copied a piece of code below. xs, ys and zs contain strings that happen to be variable names, and I'm checking to see if these variables exist using the find.. = ismember() function. Each of those strings that are variable names are variables whos values are arrays and I'd like to concatenate whatever variables do exist.
For example: if x1a and xA exist, I want to join both of those arrays to make one array stored in grids2project.x.xgrdc(i) = [x1a;xA]. (This piece of code is in a for-loop, hence the (i)). The code below just gives me the strings when i set the value = xs(findxs).
      xs = {'x1a','x2b','xA'};
    findxs = ismember(xs,who);
    ys = {'y1a','yB','y2c'};
    findys = ismember(ys,who);
    zs = {'zC','z1b','z2c'};
    findzs = ismember(zs,who);
    grids2project.x.(genvarname(['xgridc' num2str(i)],who)) = xs(findxs);
    grids2project.y.(genvarname(['ygridc' num2str(i)],who)) = ys(findys);
    grids2project.z.(genvarname(['zgridc' num2str(i)],who)) = zs(findzs);
Thanks!
댓글 수: 0
채택된 답변
  Fangjun Jiang
      
      
 2011년 7월 9일
        In this case, you need to use eval() to get its value.
a=1; VarName={'a','b','c'}; eval(VarName{1})
Use ismember() and who() is a good way to check existing variables. I wonder if you know this function. exist('a','var');
댓글 수: 0
추가 답변 (1개)
  Paulo Silva
      
 2011년 7월 9일
        Just a simple example on how to create 3 variables with predetermined values.
v={'x1a','x2b','xA'};
values={10,20,30};
for n=1:numel(v)
vn=values(n);
assignin('base',char(v(n)),vn{:})
end
댓글 수: 0
참고 항목
카테고리
				Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
			
	제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!