产生一个信息表 产生并打印一个数据表是说明函数fprintf函数就用的好方法。下面的脚本文件产生1到10中的所有整数的平方根,平方,立方,并在一个表中显示数据,并带有合适的表头。 % Script file: table.m % % Purpose: % To create a table of square roots, squares, and % cubes. % % Record of revisions: % Date Programmer Description of change % ==== ========== ===================== % 12/20/98 S. J. Chapman Original code % % Define variables: % cube -- Cubes % ii -- Indexvariable % square -- Squares % square_roots -- Squareroots % out -- Outputarray % Print the title of the table. fprintf(' Table of Square Roots, Squares, and Cubes\n\n'); % Print column headings fprintf(' Number Square Root Square Cube\n'); fprintf(' ====== =========== ====== ====\n'); % Generate the required data ii = 1:10; square_root = sqrt(ii); square = ii.^2; cube = ii.^3; % Create the output array out = [ii' square_root' square' cube']; % Print the data for ii = 1:10 fprintf (' %2d %11.4f %6d %8d\n',out(ii, ); end 程序运行后,产生的结果为 >> table Table of Square Roots,Squares, and Cubes Number Square Root Square Cube ====== =========== ========== 1 1.0000 1 1 2 1.4142 4 8 3 1.7321 9 27 4 2.0000 16 64 5 2.2361 25 125 6 2.4495 36 216 7 2.6458 49 343 8 2.8284 64 512 9 3.0000 81 729 10 3.1623 100 1000
|