Outputting to string

조회 수: 8 (최근 30일)
Sam Falzone
Sam Falzone 2011년 1월 27일
I want to do this, essentially:
for i = 1:3
['variable_' int2str(i)] = function(i);
end
How do I tell MATLAB to create a series of variables as outputs? It isn't good enough to output to variable(i) as the output is a series of matrices and vectors, not a single value.

채택된 답변

Kenneth Eaton
Kenneth Eaton 2011년 1월 27일
You can do this using the function EVAL:
for i = 1:3
eval(['var_' int2str(i) ' = fcn(i);']);
end
But in general an easier approach is to store your outputs in a cell array instead of a bunch of variables:
outputArray = cell(1,3);
for i = 1:3
outputArray{i} = fcn(i);
end
This makes it much easier to access the data later since you can just index into the cell array to get its contents, whereas for the first approach you would likely have to reconstruct a variable name and use EVAL again later to access the data stored in a particular variable.
  댓글 수: 1
Sam Falzone
Sam Falzone 2011년 1월 27일
Thank You

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Oleg Komarov
Oleg Komarov 2011년 1월 27일
Read this FAQ 4.6
Oleg

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by