许可优化
许可优化
产品
产品
解决方案
解决方案
服务支持
服务支持
关于
关于
软件库
当前位置:服务支持 >  软件文章 >  Matlab十六进制占位符与readtable:基于文件创建表

Matlab十六进制占位符与readtable:基于文件创建表

阅读数 5
点赞 0
article_banner

您可以从文件中读取十六进制和二进制数字,并将它们作为数值变量存储在表中。readtable 函数会自动读取分别包含 0x 和 0b 前缀的十六进制和二进制数字。数值使用整数数据类型存储。您也可以使用导入选项来读取不带前缀的这些数字。

在文本编辑器中预览 hexAndBinary.txt 文件。它的一些列包含带前缀的十六进制和二进制数字,还有一列包含不带前缀的这些数字。

718b6c5dcfad9daff8759251dd17a6d9.png

使用 readtable 读取文件。该函数检测具有 0x 和 0b 前缀的数字,并将它们作为整数存储。第三列没有前缀,因此其值被视为文本。

T = readtable('hexAndBinary.txt')

T=3×4 table

Var1 Var2 Var3 Var4

_____ ____ ________ ___________

255 255 {'C7F9'} {'Device1'}

256 4 {'05BF'} {'Device2'}

43981 129 {'F00F'} {'Device3'}

readtable 函数将数值存储在不同整数类(uint16 和 uint8)中,因为 T.Var1 的值需要 8 位以上的存储空间。

class(T.Var1)

ans =

'uint16'

class(T.Var2)

ans =

'uint8'

要指定用于存储从十六进制和二进制数字导入的数值的数据类型,请使用 'HexType' 和 'BinaryType' 名称-值对组参数。将值存储为有符号 32 位整数。

T = readtable('hexAndBinary.txt','HexType','int32','BinaryType','int32');

class(T.Var1)

ans =

'int32'

class(T.Var2)

ans =

'int32'

您可以使用导入选项来检测不带前缀的十六进制和二进制数字,并为它们指定存储。为 hexAndBinary.txt 创建一个导入选项对象。

opts = detectImportOptions('hexAndBinary.txt')

opts =

DelimitedTextImportOptions with properties:

Format Properties:

Delimiter: {','}

Whitespace: '\b\t '

LineEnding: {'\n' '\r' '\r\n'}

CommentStyle: {}

ConsecutiveDelimitersRule: 'split'

LeadingDelimitersRule: 'keep'

TrailingDelimitersRule: 'ignore'

EmptyLineRule: 'skip'

Encoding: 'UTF-8'

Replacement Properties:

MissingRule: 'fill'

ImportErrorRule: 'fill'

ExtraColumnsRule: 'addvars'

Variable Import Properties: Set types by name using setvartype

VariableNames: {'Var1', 'Var2', 'Var3' ... and 1 more}

VariableTypes: {'auto', 'auto', 'char' ... and 1 more}

SelectedVariableNames: {'Var1', 'Var2', 'Var3' ... and 1 more}

VariableOptions: Show all 4 VariableOptions

Access VariableOptions sub-properties using setvaropts/getvaropts

VariableNamingRule: 'modify'

Location Properties:

DataLines: [1 Inf]

VariableNamesLine: 0

RowNamesColumn: 0

VariableUnitsLine: 0

VariableDescriptionsLine: 0

To display a preview of the table, use preview

要指定第三列应作为十六进制值导入,即使缺少前缀也要导入,请使用 setvaropts 函数来修改表中第三个变量的变量类型。将变量类型设置为 int32。

opts = setvaropts(opts,3,'NumberSystem','hex','Type','int32')

opts =

DelimitedTextImportOptions with properties:

Format Properties:

Delimiter: {','}

Whitespace: '\b\t '

LineEnding: {'\n' '\r' '\r\n'}

CommentStyle: {}

ConsecutiveDelimitersRule: 'split'

LeadingDelimitersRule: 'keep'

TrailingDelimitersRule: 'ignore'

EmptyLineRule: 'skip'

Encoding: 'UTF-8'

Replacement Properties:

MissingRule: 'fill'

ImportErrorRule: 'fill'

ExtraColumnsRule: 'addvars'

Variable Import Properties: Set types by name using setvartype

VariableNames: {'Var1', 'Var2', 'Var3' ... and 1 more}

VariableTypes: {'auto', 'auto', 'int32' ... and 1 more}

SelectedVariableNames: {'Var1', 'Var2', 'Var3' ... and 1 more}

VariableOptions: Show all 4 VariableOptions

Access VariableOptions sub-properties using setvaropts/getvaropts

VariableNamingRule: 'modify'

Location Properties:

DataLines: [1 Inf]

VariableNamesLine: 0

RowNamesColumn: 0

VariableUnitsLine: 0

VariableDescriptionsLine: 0

To display a preview of the table, use preview

读取文件并将第三列作为数值(而不是文本)导入。

T = readtable('hexAndBinary.txt',opts)

T=3×4 table

Var1 Var2 Var3 Var4

_____ ____ _____ ___________

255 255 51193 {'Device1'}

256 4 1471 {'Device2'}

43981 129 61455 {'Device3'}


免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删

相关文章
技术文档
QR Code
微信扫一扫,欢迎咨询~
customer

online

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 board-phone 155-2731-8020
close1
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

姓名不为空

姓名不为空
手机不正确

手机不正确

手机不正确
公司不为空

公司不为空

公司不为空