将十六进制 C 数组转换为 Matlab vector

标签 c arrays matlab hex

我正在寻找一种从 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/

相关文章:

c# - 如何使用 Linq 的索引数组从数组中选择项目?

image - 如何将 Gabor 小波应用于图像?

R 类似于 matlab/GNU Octave 中的 str() 函数

c - 确定进程是否已死 - 通过 PID

arrays - UISearchbar 并在 View a controller xcode swift 中传递数据

java - 自定义类变量在数组中时返回 null

excel - fminsearch 在存储数据时循环覆盖

c++ - 作为函数指针的静态方法

html - CGI 不会通过 c (Eclipse) 中的 HTML 显示变量

c++ - 在 Linux 上测试网络代码的最准确方法是什么?