将十六进制数据加载到 MATLAB 时出现问题(bug?)
我正在尝试使用 load() 将以下 ascii 文件加载到 MATLAB 中
% some comment
1 0xc661
2 0xd661
3 0xe661
(这实际上是一个简化的文件。我尝试加载的实际文件在开头包含未定义的列数和未定义的注释行数,这就是加载函数有吸引力的原因)
出于某种奇怪的原因,我得到以下信息:
K>> data = load('testMixed.txt')
data =
1 50785
2 58977
3 58977
我观察到只要十六进制数字中有“d”,问题就会发生。
直接 hex2dec 转换工作正常:
K>> hex2dec('d661')
ans =
54881
importdata 似乎有相同的转换问题,ImportWizard 也是如此:
K>> importdata('testMixed.txt')
ans =
1 50785
2 58977
3 58977
这是一个错误,我是否以某种禁止的方式使用加载函数,或者是否有一些明显的我忽略的东西?
除了我自己重新实现文件解析之外,是否有解决该问题的方法?
编辑我的输入文件以更好地反映我的实际文件格式。我原来的问题有点过于简单化了。
I'm trying to load the following ascii file into MATLAB using load()
% some comment
1 0xc661
2 0xd661
3 0xe661
(This is actually a simplified file. The actual file I'm trying to load contains an undefined number of columns and an undefined number of comment lines at the beginning, which is why the load function was attractive)
For some strange reason, I obtain the following:
K>> data = load('testMixed.txt')
data =
1 50785
2 58977
3 58977
I've observed that the problem occurs anytime there's a "d" in the hexadecimal number.
Direct hex2dec conversion works properly:
K>> hex2dec('d661')
ans =
54881
importdata seems to have the same conversion issue, and so does the ImportWizard:
K>> importdata('testMixed.txt')
ans =
1 50785
2 58977
3 58977
Is that a bug, am I using the load function in some prohibited way, or is there something obvious I'm overlooking?
Are there workarounds around the problem, save from reimplementing the file parsing on my own?
Edited my input file to better reflect my actual file format. I had a bit oversimplified in my original question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
“高尔夫”答案:
这从mtrw 并进一步缩短:
上一个答案:
我的第一个想法是使用 TEXTSCAN,因为它有一个选项,允许您忽略以给定字符开头的某些行(例如
%< /代码>)。然而,TEXTSCAN 似乎不能很好地处理十六进制格式的数字。这是另一个选项:
这适用于文件开头的任意数量的注释。获取列数的计算受到 Jacob 的回答<的启发/a>.
"GOLF" ANSWER:
This starts with the answer from mtrw and shortens it further:
PREVIOUS ANSWER:
My first thought was to use TEXTSCAN, since it has an option that allows you to ignore certain lines as comments when they start with a given character (like
%
). However, TEXTSCAN doesn't appear to handle numbers in hexadecimal format well. Here's another option:This will work for an arbitrary number of comments at the beginning of the file. The computation to get the number of columns was inspired by Jacob's answer.
新:
这是我能想到的最好的。它应该适用于任意数量的注释行和列。如果有字符串等,您必须自己完成其余的
工作。这适用于这样的数据:
输出:
OLD:
是的,我不认为 LOAD 是正确的方法。你可以尝试:
New:
This is the best I could come up with. It should work for any number of comment lines and columns. You'll have to do the rest yourself if there are strings, etc.
This works for data like this:
Output:
OLD:
Yeah, I don't think LOAD is the way to go. You could try:
是“最佳品种”
这是基于 gnovice 和 Jacob 的答案,对于以下文件来说
:(其中文件中的列数必须相同,但不能提前知道,并且所有注释都用 ' 表示) %' 字符),下面的代码快速且易于阅读:
This is based on both gnovice's and Jacob's answers, and is a "best of breed"
For files like:
(where the number of columns within the file MUST be the same, but not known ahead of time, and all comments denoted by a '%' character), the following code is fast and easy to read: