how to concatenate string and number

2,390 views (last 30 days)
Hello ,
I want to concatenate string and number in for loop
requestID = Req_Check;
for k = 1 : 10
requestID = requestID +1;
I am expecting requestID after for loop like Req_Check_1, Req_Check_2 ...Req_Check_10
How can I do this?
  1 Comment
Amir Moslemi
Amir Moslemi on 2 May 2021
use "strcat" such that in each iteration convert number to string by "num2str" and eventually strcat(s1,s2). s1 your str and s2 your number(converted to string)

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 30 Jul 2015
Two (of perhaps many) possibilities:
requestIDs = 'Req_Check';
for k = 1 : 10
requestID(k,:) = [requestIDs '_' num2str(k,'%02d')]; % Character Array
requestIDs = 'Req_Check';
for k = 1 : 10
requestID{k} = [requestIDs '_' num2str(k,'%d')]; % Cell Array
The first creates a character array and produces a two-digit number filed so all are of equal length. The second uses a cell array, so the lengths don’t have to be equal.
Stephen23 on 31 Jul 2015
[D,S] = regexp('Req_Check_10','\d+','match','split')

Sign in to comment.

More Answers (1)

cesar silva
cesar silva on 18 Nov 2019
People like to make all complicated...
Concatenate 2 words
c=[a b]
Concatenate word + number
a = num2str(2)
b = 'you'
c = [b a] or... if you want... c = [b '_' a]
will give you: you2 or you_2
Daniel Posadas
Daniel Posadas on 13 Aug 2020
I'm agree with you, this one helped me out easy and fast... as a sofware engineer I hate when my programmers want to make simple things as if they were very elaborated, wasting resources. Thanks

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!