Matlab: formatting output with sprintf ()

sprintf () syntax

%Format data into string
str = sprintf(formatSpec,A1,...,An) %formats the data in arrays A1,...,An according to formatSpec in column order, and returns the results to str.

Examples

%数值的格式化输出
str_a=sprintf('%.5f',pi);

%字符串的格式化输出
str=sprintf('pi = %.5f',pi);  %输出类型为char
str1=sprintf(string('pi = %.5f'),pi);    %输出类型为string

team1='Manchester United';
team2='Chelsea';
str2=sprintf('%s won %s,congratulations',team1,team2);

The output is:

>>str_a
str_a =
3.14159

>> str
str =
pi = 3.14159

>> str1
str1 = 
  string
    "pi = 3.14159"
    
>> str2
str2 =
Manchester United won Chelsea,congratulations
Published 47 original articles · Like 33 · Visit 310,000+

Guess you like

Origin blog.csdn.net/kaever/article/details/70207380