Inserting variables into strings?

322 Ansichten (letzte 30 Tage)
Sean
Sean am 7 Jun. 2024
Beantwortet: Taylor am 2 Jul. 2024
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?
  2 Kommentare
Voss
Voss am 7 Jun. 2024
Another option:
x = 24;
y = 24.5;
str1 = "The highest number is " + x + ", and that's it!";
str2 = "What about " + y + "?";
str1,str2
str1 = "The highest number is 24, and that's it!"
str2 = "What about 24.5?"

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Taylor
Taylor am 2 Jul. 2024
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".

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by