将十六进制 C 数组转换为 Matlab 向量
Convert Hexadecimal C Array to Matlab Vector
我正在寻找一种从 C 代码十六进制数组转换为 matlab 的简单方法
可用的 C 代码格式:
const uint16_t AUDIO_SAMPLE[] = {0x4952, 0x4646, 0x4f6e, 0xf, 0x4157, 0x4556, 0x6d66, 0x2074, 0x12, 0, 0x1}
所需的 Matlab 格式:
AUDIO_SAMPLE = [18770 17990 20334 15 16727 17750 28006 8308 18 0 1]
虽然对于小数字转换很简单,但我必须转换一个非常大的数组。我正在使用嵌入式平台,因此无法将数字写入简单的文本文件以供以后在 matlab 中读取。最好用matlab写转换代码
编辑:
直到现在能够摆脱 0x。使用 eval 函数后无法获取向量,如下所示:
a='0x4952, 0x4646, 0x4f6e'; %Given
b = strrep(a, '0x', '') ; %Returns 4952, 4646, 4f6e
x = eval( [ '[', b, ']' ] ) %
将定义数组的代码复制到名为 "clip.h".
的文件中
编写一个 #include "clip.h"
的小 c 程序,并使用一个简单的循环以所需格式写入数据。 运行 桌面计算机上的程序。它可能看起来像这样:
#include <stdio.h>
#include <stdint.h>
#include "clip.h"
#define NUMBERS_PER_LINE 20
int main(void) {
int i;
int sample_length = (sizeof AUDIO_SAMPLE)/(sizeof AUDIO_SAMPLE[0]);
printf("AUDIO_SAMPLE = [ ");
for (i=0; i < sample_length; i++) {
printf("%" PRIu16 PRId16, AUDIO_SAMPLE[i]); // Use the format specifier for uint16_t.
if (i % NUMBERS_PER_LINE == NUMBERS_PER_LINE - 1) {
// Insert line continuation mark
printf(" ...\n ");
}
}
return 0;
}
该程序会将 matlab 代码写入标准输出,因此您需要将其重定向到所需的文件。
事实证明比我想象的要难。
文本文件的内容audio.c
4952, 4646, 0x4f6e, 0xf, 0x4157, 0x4556, 0x6d66, 0x2074, 0x12, 0, 0x1,
0x2, 0xbb80, 0, 0xee00, 0x2, 0x4, 0x10, 0, 0x6166, 0x7463, 0x4,
0, 0xd3cf, 0x3, 0x6164, 0x6174, 0x4f3c, 0xf, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
给定的文件每行有 11 个值。下面是将十六进制值转换为十进制数的 MATLAB 代码:
fp = fopen('audio.c', 'rt');
C = textscan(fp, '%s', 'CommentStyle', '#', 'Delimiter', '');
fclose(fp);
C = regexp(C{:}, '\w+', 'match');
C = cellfun(@(x)strrep(x,'0x',''), C, 'UniformOutput', false);
C = cellfun(@(x)hex2dec(x), C, 'UniformOutput', false);
result=cell2mat(C)'
allOneString = sprintf('%d, ' , result)
最后我得到了用逗号分隔的十进制值字符串,如下所示:
18770, 17990, 20334, 15, 16727, 17750, 28006, 8308, 18, 0, 1, 2, 48000, 0, 60928, 2, 4, 16, 0, 24934, 29795, 4, 0, 54223, 3, 24932, 24948, 20284, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
当然,之前关于 SO 的帖子提供了巨大的帮助:)
我正在寻找一种从 C 代码十六进制数组转换为 matlab 的简单方法
可用的 C 代码格式: const uint16_t AUDIO_SAMPLE[] = {0x4952, 0x4646, 0x4f6e, 0xf, 0x4157, 0x4556, 0x6d66, 0x2074, 0x12, 0, 0x1}
所需的 Matlab 格式: AUDIO_SAMPLE = [18770 17990 20334 15 16727 17750 28006 8308 18 0 1]
虽然对于小数字转换很简单,但我必须转换一个非常大的数组。我正在使用嵌入式平台,因此无法将数字写入简单的文本文件以供以后在 matlab 中读取。最好用matlab写转换代码
编辑:
直到现在能够摆脱 0x。使用 eval 函数后无法获取向量,如下所示:
a='0x4952, 0x4646, 0x4f6e'; %Given
b = strrep(a, '0x', '') ; %Returns 4952, 4646, 4f6e
x = eval( [ '[', b, ']' ] ) %
将定义数组的代码复制到名为 "clip.h".
的文件中编写一个 #include "clip.h"
的小 c 程序,并使用一个简单的循环以所需格式写入数据。 运行 桌面计算机上的程序。它可能看起来像这样:
#include <stdio.h>
#include <stdint.h>
#include "clip.h"
#define NUMBERS_PER_LINE 20
int main(void) {
int i;
int sample_length = (sizeof AUDIO_SAMPLE)/(sizeof AUDIO_SAMPLE[0]);
printf("AUDIO_SAMPLE = [ ");
for (i=0; i < sample_length; i++) {
printf("%" PRIu16 PRId16, AUDIO_SAMPLE[i]); // Use the format specifier for uint16_t.
if (i % NUMBERS_PER_LINE == NUMBERS_PER_LINE - 1) {
// Insert line continuation mark
printf(" ...\n ");
}
}
return 0;
}
该程序会将 matlab 代码写入标准输出,因此您需要将其重定向到所需的文件。
事实证明比我想象的要难。
文本文件的内容audio.c
4952, 4646, 0x4f6e, 0xf, 0x4157, 0x4556, 0x6d66, 0x2074, 0x12, 0, 0x1,
0x2, 0xbb80, 0, 0xee00, 0x2, 0x4, 0x10, 0, 0x6166, 0x7463, 0x4,
0, 0xd3cf, 0x3, 0x6164, 0x6174, 0x4f3c, 0xf, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
给定的文件每行有 11 个值。下面是将十六进制值转换为十进制数的 MATLAB 代码:
fp = fopen('audio.c', 'rt');
C = textscan(fp, '%s', 'CommentStyle', '#', 'Delimiter', '');
fclose(fp);
C = regexp(C{:}, '\w+', 'match');
C = cellfun(@(x)strrep(x,'0x',''), C, 'UniformOutput', false);
C = cellfun(@(x)hex2dec(x), C, 'UniformOutput', false);
result=cell2mat(C)'
allOneString = sprintf('%d, ' , result)
最后我得到了用逗号分隔的十进制值字符串,如下所示:
18770, 17990, 20334, 15, 16727, 17750, 28006, 8308, 18, 0, 1, 2, 48000, 0, 60928, 2, 4, 16, 0, 24934, 29795, 4, 0, 54223, 3, 24932, 24948, 20284, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
当然,之前关于 SO 的帖子提供了巨大的帮助:)