matlab:用sprintf()格式化输出

sprintf()语法

%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.

实例

%数值的格式化输出
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);

输出为:

>>str_a
str_a =
3.14159

>> str
str =
pi = 3.14159

>> str1
str1 = 
  string
    "pi = 3.14159"
    
>> str2
str2 =
Manchester United won Chelsea,congratulations

你可能感兴趣的:(matlab)