matlab disp、sprintf、fprintft函数

xiaoxiao2021-02-28  57

disp

功能:

显示变量的值。

特点:

仅接受一个输出。为了进行多个输出,可先用sprint和fprint将要输出的结果串联起来。

例子:

>> name='alice'; >> age = 12; >> x1=[name,' is ',num2str(age),' this year'];%必须用num2str将12转化为字符 >> disp(x1) alice is 12 this year >> x2=sprintf('%s is %d this year ',name,age);%sprintf 将数据格式转为字符串 >> disp(x2) alice is 12 this year

sprintf

功能:

将数据格式转为字符串

格式:

str = sprintf(formatSpec,A1,...,An) [str,errmsg] = sprintf(formatSpec,A1,...,An)

特点:

str = sprintf(formatSpec,A1,...,An) 根据 formatSpec 的列顺序设置数组 A1,...,An 中数据的格式,并将结果返回到 str

如果操作失败,[str,errmsg] = sprintf(formatSpec,A1,...,An) 将以字符向量形式返回一条错误消息。否则,errmsg 为空。

例子:

formatspec='the array is %d*%d'; >> m=2; >> n=3; >> sprintf(formatspec,m,n) ans = 'the array is 2*3'

fprintf

功能:

将数据按照指定的格式输出到屏幕上或文本文件中

格式:

fprintf(fid,format,variables)

特点:

fid:句柄、指定要写入数据的文件。若缺省则输出到屏幕上

format:输出格式

输出的时候在输出内容的末尾加上\n

例子:

转载请注明原文地址: https://www.6miu.com/read-2620328.html

最新回复(0)