MATLAB函数和命令的用法

发布时间 : 星期二 文章MATLAB函数和命令的用法更新完毕开始阅读

For %g or %G Number of significant digits. Example: '%6.4g' prints pi as ' 3.142'

Can be a number, or an asterisk (*) to refer to an argument in the input list. For example, the input list ('%6.4f', pi) is equivalent to ('%*.*f', 6, 4, pi). ? Flags

Action Left-justify. Print sign character (+ or –). Insert a space before the value. Pad with zeros. Modify selected numeric conversions: ? For %o, %x, or %X, print 0, 0x, or 0X prefix. ? For %f, %e, or %E, print decimal point even when precision is 0. ? For %g or %G, do not remove trailing zeros or decimal point. Flag Example '–' '+' ' ' '0' '#' %-5.2f %+5.2f % 5.2f .2f %#5.0f ? Identifier

Order for processing inputs. Use the syntax n$, where n represents the position of the value in the input list.

For example, '%3$s %2$s %1$s %2$s' prints inputs 'A', 'B', 'C' as follows: C B A B.

The following limitations apply to conversions:

? Numeric conversions print only the real component of complex

numbers.

? If you apply an integer or string conversion to a numeric value that

contains a fraction, MATLAB overrides the specified conversion, and uses %e.

? If you apply a string conversion (%s) to integer values, MATLAB

converts values that correspond to valid character codes to characters. For example, '%s' converts [65 66 67] to ABC.

? Different platforms display exponential notation (such as %e) with a

different number of digits in the exponent.

13

环境 示例 Windows 1.23e+004 UNIX 1.23e+04 ? 不同的操作系统环境显示的负零(-0) 不相同。

环境 %e 或 %E 转换字符 %f %g 或 %G Windows 0.000000e+000 0.000000 0 Others -0.000000e+00 -0.000000 -0 A

数值或字符数组。

示例

例1 输出多个数值和文本到屏幕。 >> B = [8.8 7.7 ; 8800 7700]; >> fprintf('X is %4.2f meters or %8.3f mm\\n', 9.9, 9900, B) X is 9.90 meters or 9900.000 mm X is 8.80 meters or 8800.000 mm X is 7.70 meters or 7700.000 mm 注:对B是按列处理。

例2 对双精度数四舍五入取整,并输出到屏幕。 >> a = [1.02 3.04 5.06]; >> fprintf('%d\\n', round(a)); 1 3 5 14

例3 写一组数据到文本文件中。 x = 0:.1:1; y = [x; exp(x)]; % 打开文件exp.txt为可写 fid = fopen('exp.txt', 'w'); fprintf(fid, '%6.2f .8f\\n', y); fclose(fid); % 查看文件内容 type exp.txt MATLAB导入功能,所有UNIX应用程序,和微软Word和写字板都把' \\ n '作为一个换行符指示器。然而,如果你要用微软记事本读取文件,请使用“\\ r \\ n”移动到新行再写。将之前的调用改写如下:

fprintf(fid, '%6.2f .8f\\r\\n', y); 输出结果不变。

例4 在Windows系统上,将PC形式的指数表示法(三位数的指数)转换到unix形式的表示法(两位数字),并打印数据到一个文件: a = [0.06 0.1 5 300] % 用sprintf转换数值数据到文本,使用%e a_str = sprintf('%e\\t',a) % use strrep to replace exponent prefix with shorter version a_str = strrep(a_str,'e+0','e+'); a_str = strrep(a_str,'e-0','e-'); %调用fprintf打印更新的文本字符串 15

fid = fopen('newfile.txt','w'); fprintf(fid, '%s', a_str); fclose(fid); % 查看文件内容 type newfile.txt 例5 在屏幕上显示一个超链接。

例6 创建一个字符矩阵并存入磁盘,再读出赋值给另一个矩阵。 >> a='string'; >> fid=fopen('d:\\char1.txt','w'); >> fprintf(fid,'%s',a); >> fclose(fid); >> fid1=fopen('d:\\char1.txt','rt'); >> b=fscanf(fid1,'%s') b = 16

联系合同范文客服:xxxxx#qq.com(#替换为@)