matlab十六进制占位符,基于文件创建表 - MATLAB readtable - MathWorks 中国

这篇博客介绍了如何在MATLAB中使用readtable函数读取包含十六进制和二进制数字的文本文件,并将其作为数值存储。通过设置'HexType'和'BinaryType'参数,可以指定存储的数据类型。同时,利用detectImportOptions和setvaropts可以处理不带前缀的十六进制数字,将其正确地作为数值导入。
摘要由CSDN通过智能技术生成

您可以从文件中读取十六进制和二进制数字,并将它们作为数值变量存储在表中。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'}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值