Matlab 和 C 动态开辟、存储和相互文件读取的测试代码

本文提供了一组测试代码,演示了Matlab和C如何动态开辟内存、处理矩阵数据,并进行文件读取交互。在Matlab中创建矩阵,然后在C++程序中读取并处理这些数据。
摘要由CSDN通过智能技术生成

% MatlabFileTest.m

a=10;
b=20;
A=zeros(a,b);

k=0;
for j=1:b
    for i=1:a
        k=k+1;
        A(i,j)=k;
    end
end
% A(i,j), where i indicates row index, j indicates collumn index

filename = sprintf('InputFile.raw');
fid = fopen(filename,'wb');
fwrite(fid,A,'double');
fclose(fid);
% save the 2D matrix column by column, i.e. A(:,1),A(:,2),...,A(:,b)
% In matlab A(i,j)=A((i-1)*Na+j)

fid=fopen(filename,'rb');
B=fread(fid,[a,b],'double');
fclose(fid);
% read the 2D matrix column by column, i.e. A(:,1),A(:,2),...,A(:,b)
% In matlab B(i,j)=B((i-1)*Na+j)

imagesc(B);
colorbar;


%%
filename = sprintf('OutputFile2');
fid=fopen(filename,'rb');
C=fread(fid,[a,b],'double');
fclose(fid);
imagesc(C);
colorbar;


------------------------------------------------------

// C++FileTest.cpp


#include <stdio.h>
#include <stdlib.h>
#include <iostream>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值