Export data from C and read it in Matlab

For 1-D data

//  Write binary files using C
int buffer[6] = { 1,2,3,4,5,6 };
FILE* write_ptr;
write_ptr = fopen("test.kt", "wb");  
fwrite(buffer, sizeof(buffer), 1, write_ptr);
//  Read binary files using Matlab
clc;
[fid,mess] = fopen('test.kt','rb')
data = fread(fid,'int');

For 2-D data !!
The reshape in Matlab is by column instead of row (in C). So, we need to read it by column using the number of original row, and then transpose it.

    int buffer[2][3] = {1,2,3,4,5,6};
    FILE* write_ptr;
    write_ptr = fopen("test_w.zkt", "wb");  // w for write, b for binary
    fwrite(buffer, sizeof(buffer), 1, write_ptr); // write 10 bytes from our buffe
    fclose(write_ptr);
[fid,mess] = fopen('test_w.zkt','rb')
B =  fread(fid, 'int');
B =  reshape(B, [], 2).'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值