Concatenate str and int and store it as a vector

조회 수: 5 (최근 30일)
Zee
Zee 2022년 5월 31일
댓글: Zee 2022년 5월 31일
Hello,
I have two arrays which I would like to concatenate with a string and save it as vector for the following data.
A= [761,2,45,65,900]
B=[56,96,368,879,56]
string = sample
I would like to concatenate the above vectors A and B with the string in the following format [sample_761_56, sample_2_96, sample_45_368, sample_65_879, sample_900_56]

채택된 답변

Jan
Jan 2022년 5월 31일
편집: Jan 2022년 5월 31일
A = [761,2,45,65,900];
B = [56,96,368,879,56];
s = 'sample';
sprintfc([s, '_%d_%d'], [A; B].')
ans = 5×1 cell array
{'sample_761_56'} {'sample_2_96' } {'sample_45_368'} {'sample_65_879'} {'sample_900_56'}
% Or:
compose('sample_%d_%d', A', B')
ans = 5×1 cell array
{'sample_761_56'} {'sample_2_96' } {'sample_45_368'} {'sample_65_879'} {'sample_900_56'}
  댓글 수: 1
Zee
Zee 2022년 5월 31일
Thanks, the first option works. Compose function doesnt work for me.

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2022년 5월 31일
S = "string";
S + "_" + A + "_" + B

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

제품


릴리스

R2011b

Community Treasure Hunt

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

Start Hunting!

Translated by