当我尝试使用 scipy (scipy.io.loadmat) 加载 matlab 文件 (.mat) 时遇到一些麻烦。数据集 (4 Go) 是波浪数值模型 (SWAN) 的输出。消息错误为:ValueError:未读取任何字节 奇怪的是,.mat 的大小为 4 Go。可能有一个解释,但我没有找到。如果有人有想法... 这是使用 mhkit 库的代码:
def _read_block_mat(swan_file):
'''
Reads in SWAN matlab output and creates a dictionary of DataFrames
for each swan output variable.
Parameters
----------
swan_file: str
filename to import
Returns
-------
dataDict: Dictionary
Dictionary of DataFrame of swan output variables
'''
assert isinstance(swan_file, str), 'swan_file must be of type str'
assert isfile(swan_file)==True, f'File not found: {swan_file}'
dataDict = loadmat(swan_file, struct_as_record=False, squeeze_me=True)
removeKeys = ['__header__', '__version__', '__globals__']
for key in removeKeys:
dataDict.pop(key, None)
for key in dataDict.keys():
dataDict[key] = pd.DataFrame(dataDict[key])
return dataDict
def read_block(swan_file):
'''
Reads in SWAN block output with headers and creates a dictionary
of DataFrames for each SWAN output variable in the output file.
Parameters
----------
swan_file: str
swan block file to import
Returns
-------
data: Dictionary
Dictionary of DataFrame of swan output variables
metaDict: Dictionary
Dictionary of metaData dependent on file type
'''
assert isinstance(swan_file, str), 'swan_file must be of type str'
assert isfile(swan_file)==True, f'File not found: {swan_file}'
extension = swan_file.split('.')[1].lower()
if extension == 'mat':
dataDict = _read_block_mat(swan_file)
metaData = {'filetype': 'mat',
'variables': [var for var in dataDict.keys()]}
else:
dataDict, metaData = _read_block_txt(swan_file)
return dataDict, metaData
使用 python 模块 scipy (io.loadmat) 读取 .mat 文件。该函数不读取任何字节,但文件不为空(4Go)
爱德华
文件已损坏。
我遇到了同样的问题,它不是一个空文件,但即使在Matlab中也无法打开它,因为文件已损坏。