Construct a reference to a matrix where the matrix name is the value of a variable
조회 수: 4 (최근 30일)
이전 댓글 표시
I want to construct a matrix reference from information contained in a set of variables. Example: say I want to set a matrix with these values:
fData = saaData(:, 16)
However, the statement is part of a for loop, and both the column and the particular matrix being referenced change on each pass. They are specified by a pair of variables:
fctSource = factors{i, 2};
fctIdx = factors{i, 3};
where on this pass, factors{i, 2} = saaData and factors{i, 3} = 16.
How can I construct a command, using fctSource and fctIdx, that will return the column vector given by saaData(:, 16)?
댓글 수: 0
채택된 답변
Teja Muppirala
2013년 4월 9일
If you need to reference a variable with a dynamic name based on a string, you'd generally call EVAL. That being said, doing these sorts of manipulations with EVAL can often get confusing, and is usually not a efficient way to solve a problem.
saaData = rand(3,16)
factors = {'something' 'saaData' 16}
if exist(factors{2},'var') %Make sure it's actually a variable...
string = [factors{2} '(:,' num2str(factors{3}) ')'];
fData = eval( string )
end
추가 답변 (1개)
Walter Roberson
2013년 4월 9일
댓글 수: 2
Walter Roberson
2013년 4월 9일
The techniques for creating a set of variables are closely related to the techniques for using a set of variables.
And you missed the big point of the beginning: DON'T DO THAT! Store your variables in indexable forms instead of using different variable names.
참고 항목
카테고리
Help Center 및 File Exchange에서 Whos에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!