How to add iteration in a string (make multiple strings using iterations without indexing) ?
23 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
the cyclist
2021 年 10 月 25 日
編集済み: the cyclist
2021 年 10 月 25 日
Here is one way:
for i = 1:4
matlab = sprintf("png%d",i)
end
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
5 件のコメント
Image Analyst
2021 年 10 月 25 日
@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.
その他の回答 (1 件)
Image Analyst
2021 年 10 月 25 日
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:
参考
カテゴリ
Help Center および File Exchange で Get Started with MATLAB についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!