从在Matlab文件读取二进制矩阵 [英] Read binary matrix from a file in Matlab

查看:827
本文介绍了从在Matlab文件读取二进制矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个二进制方阵具有复数值,存储在一个的.bin格式文件。我曾尝试用MATLAB脚本阅读本为100×100矩阵:

I have a binary square matrix with complex values, stored in a .bin format file. I have tried to read this 100-by-100 matrix with a Matlab script:

i=fopen('matrix.bin','r')
A=fread(i,[100 100]

这code不正确读取包含在 A 复杂的值。我只得到一个整数为100×100的矩阵。

This code does not correctly read the complex values contained in A. I only get a 100-by-100 matrix of integers.

推荐答案

MATLAB FREAD 支持ANSI C类型,但没有原生ANSI C类型重新present复数。最可能的是,一个复数存储为一对实数和虚数。

MATLAB fread support ANSI C types, but there is no native ANSI C types that represent complex numbers. Most likely, a complex number is stored as a pair of real and imaginary numbers.

不作为二进制文件是如何保存的信息,仍然可以进行一些测试摸不着头脑。如果复数是psented作为实部和虚部重新$ P $,并且都在 precision话单复数将占用8 + 8 = 16个字节。我们可以导航到该文件的末尾进行测试,看看有多少个字节有。

Without information as to how the binary file is saved, you can still perform some test to figure this out. If the complex number is represented as a real part and an imaginary part, and both in double precision, then a single complex number would take up 8 + 8 = 16 bytes. We can test this by navigating to the end of the file, and see how many bytes there are.

fID = fopen('matrix.bin','r')
fseek(fID, 0, 'eof')      % Go to the end of file
ftell(fID)                % Tell current position in the open file
fclose(fID)

如果这个数字等于16 * 100 * 100 = 16万,那么你很幸运。目前保存在这个文件中没有多余的东西,你可以简单地通过这个code读取数据:

If this number is equal to 16 * 100 * 100 = 160000, then you're in luck. There is no extra stuff saved in this file, and you can simply read the data by this code:

fID = fopen('matrix.bin','r')
data = []
for ii = 1:10000
    data = [data; fread(fID, 2, 'double')']
end
fclose(fID)

您会拥有一个10000 * 2阵列,每行再presenting复数。如果文件大小为80000,那么这两个实部和虚部可以被保存在数据类型。如果文件大小是一些其它数量的,则这可能意味着一些额外的信息被存储在二进制。你必须要知道更多的信息被存储什么,所以你可以正确读取该文件。

You'll end up with a 10000*2 array, with each row representing a complex number. If the file size is 80000, then both real and imaginary part could be saved in single data type. If file size is some other number, then it probably means some additional information is stored in the binary. You'll have to know what additional information is stored so you can read the file correctly.

这篇关于从在Matlab文件读取二进制矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆