Inserting variables into strings?

321 次查看(过去 30 天)
Sean
Sean 2024-6-7
回答: Taylor 2024-7-2
In Charp I regulaly use
string newstring = $"This string is really easy to change as needed - since just put any data type here - {datatype} the complier takes care of it and consolidates to a single string.";
Am I missing something, is there a way to do this in matlab?

回答(1 个)

Taylor
Taylor 2024-7-2
The modern "string" datatype is much more intuitve in my opinion, so I recommend Voss' approach over using "fprintf" or "sprintf". It also allows for vectorized usage as well:
x = 1:5;
str = "myFile";
str + x
ans = 1x5 string array
"myFile1" "myFile2" "myFile3" "myFile4" "myFile5"
It is also much more capable when it comes to text analytics as well. This was actually the motivation behind creating the new "string" class instead of just using "char".

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by