您的位置:首页 > 编程语言 > MATLAB

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

2012-10-20 04:42 375 查看
% 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>

using namespace std;

int main(void) {

int Na=10;
// height of the detector

int Nb=20;
// width of the detector

FILE *fp;

char filename[500];

double P;

int i,j,k,m;

/******Method 1***********/

// dynamiclly create a 2D matrix Na*Nb

double ** Matrix = new double*[Na+1];

for(int i = 0; i < Na+1; ++ i)

Matrix[i] = new double[Nb+1];

sprintf(filename,"InputFile.raw");

if((fp=fopen(filename,"rb"))==NULL)

{

printf("cannot find the Matrix file");

exit(0);

}

// Read one by one

for (j=1;j<=Nb;j++)

for (i=1;i<=Na;i++)

{

fread(&P,sizeof(double),1,fp);

Matrix[i][j]=P;

printf("%f\n",Matrix[i][j]);

}

fclose(fp);

//Write Matrix to File

sprintf(filename,"OutputFile1");

if((fp=fopen(filename,"wb"))==NULL)

{

printf("cannot find the Input file");

exit(0);

}

// write one by one

for (int j=1;j<=Nb;j++)

for(int i=1;i<=Na;i++)

{

if(fwrite(&Matrix[i][j],sizeof(double),1,fp)!=1)

printf("file write error\n");

}

fclose(fp);

// delete

for (i = 0; i < Na+1; i++)

{

delete []Matrix[i];

Matrix[i] = NULL;

}

delete []Matrix;

Matrix = NULL;

/*******Method 2**********/

// dynamiclly create a 2D matrix Na*Nb

double ** Matrix2 = new double*[Nb+1];

for(int i = 0; i < Nb+1; ++ i)

Matrix2[i] = new double[Na];

sprintf(filename,"InputFile.raw");

if((fp=fopen(filename,"rb"))==NULL)

{

printf("cannot find the Input file");

exit(0);

}

// read collumn by collumn

for (int i=1; i<=Nb; ++i)

{

fread(&(Matrix2[i][0]),sizeof(double)*Na,1,fp);

printf("%f\n",Matrix2[i][0]);

}

fclose(fp);

// You can not do it like this :

// fread(&Matrix2[0][0],sizeof(double)*Na*Nb,1,fp);

sprintf(filename,"OutputFile2");

if((fp=fopen(filename,"wb"))==NULL)

{

printf("cannot find the output file\n");

exit(0);

}

// write row by row

for (int i=1; i<=Nb; ++i)

fwrite(&Matrix2[i][0],sizeof(double)*Na,1,fp);

fclose(fp);

// delete

for (i = 0; i < Nb+1; i++)

{

delete []Matrix2[i];

Matrix2[i] = NULL;

}

delete []Matrix2;

Matrix2 = NULL;

// Test

puts("Hello World"); /* prints Hello World */

return EXIT_SUCCESS;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: