我正在寻找一种从 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 函数后无法获取 vector ,如下所示:
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 代码写入标准输出,因此您需要将其重定向到所需的文件。
关于将十六进制 C 数组转换为 Matlab vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32089100/