How does one produce an array of strings automatically?
조회 수: 4 (최근 30일)
이전 댓글 표시
I wish to produce an array/vector of strings to use as InputNames and OutputNames on a dynamical system model in something like the following manner (note that the below doesn't work, but illustrates what I'm looking for):
n = 5;
stringVector = {'v' num2str(n)};
in order to produce a vector of strings as such:
stringVector = {'v1', 'v2', 'v3', 'v4', 'v5'}
How can this be done?
Thanks, Olie
댓글 수: 0
채택된 답변
Mischa Kim
2014년 4월 14일
편집: Mischa Kim
2014년 4월 14일
Oliver, you could do
n = 5;
for ii = 1:n
stringVector{ii} = strcat('v',int2str(ii));
end
or simply
stringVector = strcat({'v'},int2str((1:5)'));
댓글 수: 3
추가 답변 (2개)
Jan
2014년 4월 14일
There is an undocumented but extremely efficient function for this job:
stringVector = sprintfc('v%d', 1:5)
댓글 수: 0
Sean de Wolski
2014년 4월 16일
And a documented one liner:
x = strcat('v',cellstr(num2str((1:5).')))
댓글 수: 1
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!