MatLab---變量的顯示與輸出(中)


1.input輸入

v=input('Enter a vector: ')  輸入的是一個數組
Enter a vector: [12 5 6 78]

v =

12 5 6 78

2.output輸出

1)fprintf()

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 %s,for sure!\n',[64 65]) 占位符是字符串,即多個字符
the value is @A,for sure!
>> fprintf('the value is %c,for sure!\n',[64 65]) 當占位符是字符,而賦值是字符串的時候,顯示如下:
the value is @,for sure!
the value is A,for sure!

>> fprintf('line 1\nline 2\n\nline4\n')
line 1
line 2

line4

>> fprintf('|%6d|\n',randi([1,1000],[10,1]))  占6個字符的位置
|   656|
|     36|
|   850|
|   934|
|   679|
|   758|
|   744|
|   393|
|   656|
|   172|

fprintf('|%8.3f|\n',rand([1,10]))  一共占8個字符,小數點后有3位,小數點也算一位
|    0.706|
|    0.032|
|    0.277|
|    0.046|
|    0.097|
|    0.823|
|    0.695|
|    0.317|
|    0.950|
|    0.034|

fprintf('|%6d|\n',123456.78) 當前后的數值類型不一致時,顯示科學計數法
|1.234568e+05|

fprintf('|%-6d|\n',randi([1,1000],[10,1]))  在占位符的位置,加入-,居左顯示
|76      |
|54      |
|531    |
|780    |
|935    |
|130    |
|569    |
|470    |
|12      |
|338    |

fprintf('|%+6d|\n',randi([-1000,1000],[10,1]))  顯示數值的正負號
|     -99|
|   -833|
|   -542|
|  +827|
|   -696|
|  +652|
|    +77|
|  +993|
|   -844|
|   -115|

fprintf('|%s|','street') 
|street|

>> fprintf('|%10s|\n','street')
|     street|
>> fprintf('|%4s|\n','street')  雖然占位符是4,仍然顯示6個字符
|street|
>> fprintf('|%.4s|\n','street')  當占位符前面加入.時,截取前面4個字符
|stre|
>> fprintf('%f\t%f\t%f',rand(),rand(),rand())  制表符的使用,每個計算機的制表符可能長度不同
0.431414 0.910648 0.181847

3.特殊字符單引號  '   與反斜杠  \ 的顯示

fprintf('   ' '   \n')  輸入兩個單引號
'

fprintf('\\ \n')   輸入兩個反斜杠
\


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM