变量的输入和输出,控制数值的输出位数,注释,矩阵的逆,num2str功能,input命令

该文介绍了MATLAB中如何使用disp、fprintf和num2str函数来显示和格式化变量的输出,包括计算圆面积的例子,以及处理用户输入数据的不同方法,如输入数值和字符。还涉及到矩阵的逆和不同精度的显示设置。
摘要由CSDN通过智能技术生成
变量的输出和显示

注释:选中要注释的内容,ctrl+r
%{ comment1
comment2
comment3
comment4%}
撤销注释:ctrl+t

radius = 5;
area=pi*radius^2;
disp(area);
>> script1
   78.5398
radius = 5;
area=pi*radius^2;
disp(['the area of the disc is ' area]);
>> script1
the area of the disc is N
radius = 5;
area=pi*radius^2;
disp(['the area of the disc is ' num2str(area)]);
>> script1
the area of the disc is 78.5398
radius = 5;
area=pi*radius^2;
fprintf('the area of the disc is %f \n',area);
>> script1
the area of the disc is 78.539816 
radius = 5;
area=pi*radius^2;
fprintf('the area of the disc is %.2f \n',area);
>> script1
the area of the disc is 78.54 

%d,用科学计数法控制输出

radius = 5;
area=pi*radius^2;
fprintf('the area of the disc is %d\n',area);
>> script1
the area of the disc is 7.853982e+01

num2str函数用法
T = num2str(X)
功能:把数组X中的数转换成字符串表示形式。

>> A = [1, 2, 3];
B = num2str(A);
disp(B)
1  2  3

T= num2str(X,N)
功能:把数组C转换成字符串形式表示,precision表示精度, 比如precision为2表示保留最多2位有效数字, 例如1.564转换后为1.6(四舍五入),0.12345转换后为0.12。即从左边第一个不为0的数开始保留2个数值。

 >> C = [1.564, 0.12345];
T = num2str(C,2);
disp(T)
1.6     0.12

T = num2str(X,FORMAT)
功能:按format指定格式进行格式化转换,%.6f代表小数点后6位。

 C = [1.564 , 0.12345];
>> T2 = num2str(C, '%.6f ')

T2 =

  '1.564000 0.123450'

有关矩阵的逆

在这里插入图片描述

%the script caculates the area of a circle
%the radius is assigned
radius = 5;
%the area is caculate base on the radius
area=pi*radius^2;
>> help Untitled2
the script caculates the area of a circle
the radius is assigned
>> r=input('Enter the radius: ')
Enter the radius: 5
r =
     5
>> letter=input('Enter a char: ','s')
Enter a char: 5

letter =

    '5'

>> whos
  Name               Size            Bytes  Class     Attributes

  circumference      1x1                 8  double              
  letter             1x1                 2  char                
  r                  1x1                 8  double              
  radius             1x1                 8  double   
>> R=16
R =
    16
>> r=input('Enter the radius: ')
Enter the radius: R
r =
    16
>> letter=input('Enter a char: ','s')
Enter a char: 
letter =
  空的 0×0 char 数组
>> letter = input('Enter a char: ','s')
Enter a char:          go
letter =
    '         go'
>> letter1 = input('Enter a char: ')
Enter a char: 'apple'
letter1 =
    'apple'
>> letter2 = input('Enter a char: ')
Enter a char: "apple"
letter2 = 
    "apple"
>> letter3 = input('Enter a char: ','s')
Enter a char: apple
letter3 =
    'apple'
>> letter4 = input('Enter a char: ','s')
Enter a char: 'apple'
letter4 =
    ''apple''
>> whos
  Name               Size            Bytes  Class     Attributes
          
  letter1            1x5                10  char                
  letter2            1x1               134  string              
  letter3            1x5                10  char                
  letter4            1x7                14  char     

>> v=input('Enter a vector: ')
Enter a vector: [1 2 34 567]
v =
   1     2    34   567

%c输出字符,%s输出字符串

>> fprintf('The value is %d,for sure!\n',4^3)
The value is 64,for sure!
>> fprintf('The value is %f,for sure!\n',4^3)
The value is 64.000000,for sure!
>> fprintf('The value is %c,for sure!\n',4^3)
The value is @,for sure!
>> fprintf('The value is %s,for sure!\n',4^3)
The value is @,for sure!
>> fprintf('The value is %c,for sure!\n',[64 65])
The value is @,for sure!
The value is A,for sure!
>> fprintf('The value is %s,for sure!\n',[64 65])
The value is @A,for sure!

输出1-1000的整数,最小宽度为6,不够6位向左补空格

>> fprintf('|%6d|\n',randi([1,1000],[10,1]))
|    76|
|    54|
|   531|
|   780|
|   935|
|   130|
|   569|
|   470|
|    12|
|   338|
>> fprintf('|%d|\n',randi([1,1000],[10,1]))%不够整齐
|707|
|32|
|277|
|47|
|98|
|824|
|695|
|318|
|951|
|35|

输出0-1的数,保留小数点后三位,最小宽度为8,不够8位向左补空格

>> fprintf('|%8.3f|\n',rand([10,1]))
|   0.162|
|   0.794|
|   0.311|
|   0.529|
|   0.166|
|   0.602|
|   0.263|
|   0.654|
|   0.689|
|   0.748|
>> fprintf('|%8f|\n',rand([10,1]))
|0.450542|
|0.083821|
|0.228977|
|0.913337|
|0.152378|
|0.825817|
|0.538342|
|0.996135|
|0.078176|
|0.442678|
>> fprintf('|%8f|\n',randi([1,3],[10,1])+rand([10,1]))
|1.431414|
|3.910648|
|1.181847|
|3.263803|
|3.145539|
|3.136069|
|1.869292|
|2.579705|
|1.549860|
|3.144955|
>> fprintf('|%8f|\n',randi([1,300],[10,1])+rand([10,1]))
|256.417267|
|187.049654|
|106.902716|
|154.944787|
|121.490864|
|23.489253|
|72.337719|
|37.900054|
|56.369247|
|72.111203|

输出0-1000的数,最小宽度为10,不够10位向左补空格

>> fprintf('|%10d|\n',randi([1,1000],[10,1]))
|       548|
|       297|
|       745|
|       189|
|       687|
|       184|
|       369|
|       626|
|       781|
|        82|

向左对齐

>> fprintf('|%-10d|\n',randi([1,1000],[10,1]))
|930       |
|776       |
|487       |
|436       |
|447       |
|307       |
|509       |
|511       |
|818       |
|795       |

显示正负号

>> fprintf('|%10d|\n',randi([-1000,1000],[10,1]))
|      -378|
|       847|
|      -140|
|      -631|
|       810|
|       960|
|      -122|
|      -778|
|      -484|
|      -183|
>> fprintf('|%+10d|\n',randi([-1000,1000],[10,1]))
|      +190|
|      -476|
|      +206|
|      +423|
|      -557|
|      -766|
|      -407|
|      -363|
|      -152|
|       +16|
>> fprintf('%s\n','street')
street
>> fprintf('%10s\n','street')
    street
>> fprintf('|%10s|\n','street')
|    street|
>> fprintf('|%4s|\n','street')
|street|
>> fprintf('|%.4s|\n','street')
|stre|
>> fprintf('%f\t%f\t%f\n',rand( ),rand( ),rand( ))
0.963089	0.546806	0.521136

在这里插入图片描述

>> fprintf('\\ \n')
\ 
>> fprintf('\ \n')
警告: 转义字符 '\ ' 无效。有关支持的特殊字符,请参阅 'doc sprintf'。 
>> fprintf(''\n')
未定义函数或变量 'n'。 
>> fprintf('''\n')
'

应用实例

fprintf('Note:the units will be inches.\n');
radius=input('Please enter the radius: ');
area=pi*radius^2;
fprintf('For a circle with a radius of %.2f inches,the area is %.2f inches\n',radius,area);
>> Untitled3
Note:the units will be inches.
Please enter the radius: 5
For a circle with a radius of 5.00 inches,the area is 78.54 inches
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值