How to add iteration in a string (make multiple strings using iterations without indexing) ?

I have been wondering for a loooong time how to acheive this. (I am a beginner by the way so please bear with me)
The problem is shown in the sample code below. I want the numerical "i" to be used in the string to output: "png1", "png2", and so forth.
Of course the code doesn't work.
I know of a way; to make a matrix in advance with string then access the matrix but this won't work with a while loop.
I thank you so much in advance.
for i = 1:4
matlab = "png(i)"
end

 Accepted Answer

Here is one way:
for i = 1:4
matlab = sprintf("png%d",i)
end
matlab = "png1"
matlab = "png2"
matlab = "png3"
matlab = "png4"
Here is another, but wanted to show the first way, which can be more versatile for other ways to build strings and/or character arrays.
for i = 1:4
matlab = "png"+i
end
matlab = "png1"
matlab = "png2"
matlab = "png3"
matlab = "png4"

5 Comments

I THANK YOU SO MUCH. I was going insane trying to figure this out.
I have something to say about second way, I was trying to add i as a char, I didn't think of adding it as a string, as changing a numerical to a string copys it.
Again, THANK YOU SO MUCH <3
@Haisam Khaled, another way is to make a cell array of strings:
stringList = cell(4, 1); % Preallocate list of empty cells.
% Fill that list with strings (actually character arrays here,
% but you could do strings if you want).
for k = 1 : 4
stringList{k} = sprintf('png%d', k) % Character array type.
%stringList{k} = "png" + k; % String type
end
See the FAQ:
If you want to create a 4 element string array you don't need to use a loop. Adding a string scalar and a numeric vector converts the numeric vector to a string array of the same size then adds on that string scalar to each element. You can even use implicit expansion.
i = (1:4).';
matlab = "png"+i
matlab = 4×1 string array
"png1" "png2" "png3" "png4"
y = matlab + i.'
y = 4×4 string array
"png11" "png12" "png13" "png14" "png21" "png22" "png23" "png24" "png31" "png32" "png33" "png34" "png41" "png42" "png43" "png44"
@Steven Lord, neat trick. Good to know. Thanks for sharing.
matlab is a reserved variable name so he should use a different variable name. Just type matlab on the command line then dot and and the tab key to see all the methods and properties of the matlab object/class. Another neat trick.
@Image Analyst DUUUDE this was the one that started it all, I saw it once somewhere, but somehow forgot it and no matter what I tried, I never thought of this one. THANK YOU AGAIN, now I have immense knowledge <3

Sign in to comment.

More Answers (1)

If I understand you correctly you want to dynamically/programmatically create named variables and then access them later by that created name. The reasons against this are discussed in the FAQ:

1 Comment

Hey man, first of all, I am a big fan of you, you answered many questions of other people that I benefitted a lot from.
Second, I know my mind is that of a beginner and yours is so advanced and much smarter than me. I didn't explain the problem well, regaardless someone understood my stupid explanation and provided a solution, I thank you soooo much for your effort.
P.S. this article was helpful <3

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2020a

Community Treasure Hunt

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

Start Hunting!