我目前正在尝试在Matlab中将一个数组保存为二进制文件,发送到Python并在Python中读取它。然而,在运行时,Matlab显示错误。我正在使用以下代码:
在Matlab中读取数组,转换为二进制文件并传递给Python:
array1 = rand(5,1); %% array1 is the desired array that needs to be sent to Python
fid = fopen('nazmul.bin','wb'); %% I want to save array1 in the nazmul.bin file
fwrite(fid,array1);
status=fclose(fid);
python('squared.py','nazmul.bin'); %% I want to send the parameters to squared.py program
squared.py文件:
import sys
if __name__ == '__main__':
f = open("nazmul.bin", "rb") # Trying to open the bin file
try:
byte = f.read(1) # Reading the bin file and saving it in the byte array
while byte != "":
# Do stuff with byte.
byte = f.read(1)
finally:
f.close()
print byte # printing the byte array from Python
然而,当我运行这个程序时,没有任何内容被打印出来。我猜测二进制文件没有被正确地传递给squared.py文件。谢谢您的反馈。Nazmul