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;
}
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;
}
相关文章推荐
- opencv读取彩色/灰度图片像素值并存储在本地文件中c++代码实例及运行结果
- C 创建链表并将信息存储在二进制文件中读取的实例代码
- opencv读取彩色/灰度图片像素值并存储在本地文件中c++代码实例及运行结果
- C和C++开辟指针数组读取文件的测试
- Matlab R2014a 由.m文件产生C代码,并在VS2010中测试
- Android实现文件存储并读取的示例代码
- opencv读取彩色/灰度图片像素值并存储在本地文件中c++代码实例及运行结果
- JXL读取Excel文件测试代码
- opencv读取彩色/灰度图片像素值并存储在本地文件中c++代码实例及运行结果
- 测试用指定块大小读取文件时间的代码
- opencv读取彩色/灰度图片像素值并存储在本地文件中c++代码实例及运行结果
- matlab 读取文件(mat)存储为json文件
- opencv读取彩色/灰度图片像素值并存储在本地文件中c++代码实例及运行结果
- pgm格式文件及其在matlab中的读取存储方法(2)
- Matlab | 算法运行过程中间项的文件存储与读取
- Matlab读取BMP文件代码
- opencv读取彩色/灰度图片像素值并存储在本地文件中c++代码实例及运行结果
- C语言实现电话本 动态开辟 信息存储于文件
- opencv读取彩色/灰度图片像素值并存储在本地文件中c++代码实例及运行结果
- TensorFlow基础5:TFRecords文件的存储与读取讲解及代码实现