将matlab结构导入python

2024-10-02 12:37:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python非常陌生,我必须对工作中的现有项目进行一些更改。我正在将Matlab二进制数据文件中的数据导入到我的Python脚本中。在

假设原始*.m文件包含如下内容:

> Temperature=61.3
> 
> VibrationSamples=[76,75,76,77, ... a lot of samples, 78]
>
> save(filename.mat, 'Temperature', 'VibrationSamples', '-mat7-binary')

(这是我能提供的最简单的例子)

我使用

^{pr2}$

之后,我可以使用

> variableA = mat('temperature')
> variableB = mat('vibrationSamples')

但是我对结果感到困惑。如果我尝试打印varialbeA,例如,它将得到二维数组。print variableA的输出是[[61.3]]。在*.m文件中,它显然是float。在

为什么会这样。什么是干净的解决方法?我知道我可以用

> variableA = mat('temperature')[0][0]

但这看起来并不合适。在

我做错什么了吗?在


Tags: 文件数据项目脚本内容数据文件二进制lot
1条回答
网友
1楼 · 发布于 2024-10-02 12:37:16

这就是MATLAB存储标量的方式。在MATLAB数据模型中,标量用1×1数组表示。在

所以在Python中,您可以像这样提取值:

mat['Temperature'][0,0]

关于这个问题的MATLAB方面的更多信息可以在这里找到:http://www.mathworks.co.uk/help/matlab/math/empty-matrices-scalars-and-vectors.html#f1-86433

Scalars

Any individual real or complex number is represented in MATLAB as a 1-by-1 matrix called a scalar value:

A = 5;

ndims(A)        % Check number of dimensions in A
ans =
     2

size(A)         % Check value of row and column dimensions
ans =
     1     1

Use the isscalar function to tell if a variable holds a scalar value:

isscalar(A)
ans =
    1

相关问题 更多 >

    热门问题