# MATLAB 基础知识 数据类型 数据类型转换 将数值转换为文本

## 转换为字符串

``````str = string(pi)
str =
"3.1416"
``````

string 函数将数值数组转换为大小相同的字符串数组。

``````A = [256 pi 8.9e-3];
str = string(A)
str = 1x3 string
"256"    "3.141593"    "0.0089"
``````

``````str = compose("%9.7f",pi)
str =
"3.1415927"
``````

``````A = [256 pi 8.9e-3];
str = compose("%5.2e",A)
str = 1x3 string
"2.56e+02"    "3.14e+00"    "8.90e-03"
``````

## 将数值添加到字符串

``````X = linspace(0,2*pi);
Y = sin(X);
plot(X,Y)
freq = 1/(2*pi);
str = "Sine Wave, Frequency = " + freq + " Hz"
str =
"Sine Wave, Frequency = 0.15915 Hz"
title(str)
``````

``````str = 'Sine Wave, Frequency = ' + string(freq) + { ' Hz'}
str =
"Sine Wave, Frequency = 0.15915 Hz"
``````

## 字符代码

``````u = [77 65 84 76 65 66];
c = char(u)
c =
'MATLAB'
``````

``````deg = char(176);
temp = 21;
str = "Temperature: " + temp + deg + "C"
str =
"Temperature: 21°C"
``````

``````str = ['Temperature: ' num2str(temp) deg 'C']
str =
'Temperature: 21°C'
``````

## 十六进制和二进制值

``````register = 0b10010110
register = uint8
150
register = bitset(register,5,0)
register = uint8
134
binStr = dec2bin(register)
binStr =
'10000110'
``````
原文作者：结冰架构
原文地址: https://blog.csdn.net/u013883025/article/details/117988570
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。