matlab串口用fprintf格式化给51单片机串口通信程序发送数据 用%s和%d这两种格式发送单片

matlab fprintf 中%f 和%d 的区别_百度知道您所在的位置: &
教程:MATLAB输出结果的格式设置
教程:MATLAB输出结果的格式设置
吴文国 林川 译
清华大学出版社
《MATLAB & Excel工程计算》结合了当前工程计算领域里两个最重要的计算工具――MATLAB和Excel,它们是工程专业学生为解决未来工作中遇到的实际工程问题而必须掌握的计算工具。第4章讲述MATLAB编程。本节说的是教程:MATLAB输出结果的格式设置。
4.5& 教程:MATLAB输出结果的格式设置
在本章的前面几节里,我们用fprintf( )函数把文本消息输出到屏幕上。在这一节,读者将要学习用fprintf( )函数把变量的值输出到屏幕上或一个结果文件里,还要学习如何对这些变量的值和相应的文字说明进行格式设置。
fid是文件标识符,当结果输出到屏幕时,可以省略fid。单引号之间的内容是每个变量的输出格式转换说明。每个输出格式说明以%开始,后面紧跟与该变量有关的字符串(可选)、小数位数和格式类型。格式类型有:
f=定点小数表示
e或E=科学表示法
i=整数表示法
例如,%8.1e表示以科学表示法输出该变量的值,整个值占用8个字符,小数位数取1位。
字符串可以包含像\n这样的特殊字符,它表示换到下一行。还有其他几个特殊字符,如\t表示tab。在MATLAB的帮助文档中搜索fprintf( )的用法,就会看到有关fprintf( )函数的各种格式选项。对于大多数应用,本节介绍的这些选项已经足够了。
为了说明fprintf( )函数的用法。我们分析表4-2中所示的例子。在这些例子里,m=12, d=。
在前一节的Triangle脚本程序里,每个三角形由边长x、y和h确定。每找到一个三角形,我们都希望把三边的长度显示在同一行里。
在Triangle脚本里,找到在屏幕上输出x、y和h的行,用一个fprintf( )函数把它们输出到同一行里。此外,修改最后一行,这一行输出计数器m的值。其他值都不变。
表4-2& fprintf命令的示例
需要注意的是,第22行的"\n"字符的作用是,在输出每个三角形的x、y和h的值后,自动换行到下一行。在第26行里,字符串的开始有一个"\n"字符,这会使在最后一个三角形输出后,跳过一行,输出这个字符串。而字符串后面的"\n",会使提示符显示在下一行的行首。
保存修改后的脚本程序,在命令窗口里,输入clc命令,清除屏幕,运行Triangle脚本程序,结果如图4-22所示。
我们很容易把屏幕上的输出结果复制到Word或Excel文档里。但是,如果在屏幕上输出大量的数据,我们有时候希望直接把数据保存到一个结果文件里。为此需要先定义文件名。这可以用fopen命令实现。定义一个文件名的方法如下:
把filename这个文件的标示符赋给变量fid(使用其他变量也可以)表示。如果生成的是一个文本文件,最好在文件名后加上.txt扩展名。这样,当我们在Windows里双击这个文件名,就可以用默认的文本编辑器(通常是记事本程序)打开这个结果文件。文件的属性(attribute)用wt表示,它表示写入(write)文本文件(text)。如果这个文件还不存在,执行这个命令后,就会建立这个文件。如果系统中已存在这个文件,则它的全部内容会被新写入的内容覆盖。如果我们希望在一个已经存在的文件的末尾添加内容,要使用at属性。
现在要把Triangle程序的结果保存到output.txt文件里,需要把这个程序修改成如下的结果:
第9行的fopen命令建立一个具有写入属性的文件。第10行和第11行用于写入列标题。因为列标题只需写入一次,因此必须放在循环之前。fprintf( )函数的第一个参数是fid,表示把变量输出到文件里,而非屏幕上。此外在第26行里,增加了fid标识符。注意当我们用fprintf( )函数把输出结果保存到文件时,最后要在行尾加一个分号。否则会在命令窗口显示写入到文件里的字符个数。第30行输出符合要求的三角形个数。这里的fprintf( )函数并没有fid这个标识符,因此,它会把输出结果显示在屏幕上,而不是结果文件里。
保存并运行这个脚本文件。
现在只有三角形的个数显示在屏幕上,如图4-23所示。
我们注意到,在当前目录窗口里多了一个output.txt文件,如图4-24所示。这个文件可以用记事本、Word或Excel,甚至用MATLAB的编辑器打开。要想在MATLAB的编辑器里打开这个文件,只需要双击当前目录窗口的文件名。
这个文件的内容如图4-25所示。
最后,把脚本中第13行和第14行的x和y的上限值修改为1000,保存并运行这个文件。
在命令窗口里,我们看到有456个边长为整数的三角形符合要求。如果打开output.txt文件,就会在这个文件里看到全部的456个答案。
【责任编辑: TEL:(010)】&&&&&&
关于&&&&的更多文章
《分布式服务框架原理与实践》作者具有丰富的分布式服务框架、平
本书描述了黑客用默默无闻的行动为数字世界照亮了一条道路的故事。
讲师: 26人学习过讲师: 14人学习过讲师: 35人学习过
本书是在MIT开设概率论入门课程的基础上编写的, 内容
在传统的异构网络环境中,运维人员往往利用各种复杂的
本书共4部分,首先简要介绍了开发相关的基础知识,然
本书是作者根据十年网站营销经验,特别是搜索引擎优化(SEO)实现营销方面的技术和实践总结。本书帮助读者建立搜索营销的概念,
51CTO旗下网站The fprintf function
The fprintf function is used for printing information to the
screen. The fprintf function prints an array of characters to the
fprintf('Happy Birthday\n');
We often use the fprintf statement to show the user information stored in
our variables.
To "place" a number into this string of printed
characters we use several formatting options, but they all start with a
% sign. It should noted that when used in this way, the % is not the
comment character.
Below are several examples of printing information from variables
using fprintf.
Notice the use of %s to print a string, and %d to print
an integer, and %f to print a number with a decimal (a floating point
The Value of each variable is "plugged into"
the appropriate format string at the appropriate location as marked by
the % sign in the string
fprintf('My name is %s\n', prof_name);
fprintf('My age is %d and my salary is $%.2f\n', prof_age, prof_salary);
Again, the '%' notation in this case is not a comment, but is
the "formating" of the string.
When using the % signs to print out the data stored in variables, we
must use the same number of % signs as the number of variables.
Some common format options:
%s - print a string
%c - print a single character
%d - print a whole number
%f - print a floating point number
\n - print a new line (go to the next line to continue printing)
\t - print a tab
\\ - print a slash
%% - print a percent sign
Above, the example "%.2f" is given.
By putting a decimal point and a
number in the format command, you are telling the program to print only
two decimal places.
This is perfect for printing dollar amounts, where
we don't care about 1/1000 ths of a cent.
For more information, try "doc fprintf"
Alternative ways to print information
The following ways to "print" information are also available in
Matlab, but are "frowned" upon, except for rapid debugging of a
The display command:
disp is a function that will display a value
name = 'Jim';
disp(name);
alternative to the fprintf statement.
prefer to use fprintf as we control theFormatting
Simply removing the ';' semicolon from the variable assignment:
forget the ';'
name = 'Jim'
Do not use this option, except for quick debugging
Again, please note: fprintf is the preferred method
When to print information.
Any time you print information, you should make sure it is of
interest to the user of the program every time the code is run..
For example a function that compute pi should
never use fprintf.
Instead, such a function should return the
value of pi for use by other parts of the program.
approximated value of pi is of interest to the user, then the main
program can print the result.
Proper use of fprintf to display the result of a function
function result = add(a,b)
result = a +
&& the_sum = add(5,7);
&& fprintf('the sum is %d\n', the_sum);
Improper use of fprintf to display the result of a function
function result = add(a,b)
result = a +
fprintf('the sum is %d\n', the_sum);
&& the_sum = add(5,7);
the sum is 12
&& fprintf('the sum is %d\n', the_sum);
the sum is 12
Notice how the second example prints "the sum is XX" every time the
add function is called.
In a program that adds millions of numbers,
this would become quite annoying very quickly.
It is very important NOT to use fprintf in a function when the
reason for using the function is to compute a value for the computer to
It is very annoying to have miscellaneous printouts from
un-related functions, when trying to compute a final value. 上传我的文档
 下载
 收藏
该文档贡献者很忙,什么也没留下。
 下载此文档
正在努力加载中...
Matlab与单片机的串行通信及数据处理
下载积分:600
内容提示:Matlab与单片机的串行通信及数据处理 11:06摘要:结..
文档格式:PDF|
浏览次数:344|
上传日期: 01:40:55|
文档星级:
该用户还上传了这些文档
Matlab与单片机的串行通信及数据处理
官方公共微信}

我要回帖

更多关于 单片机格式化输出 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信