在Matlab中从Python脚本加载变量

1 投票
0 回答
42 浏览
提问于 2025-04-12 05:15

我想知道怎么在matlab中使用一个已经在.py脚本中定义的python变量。

具体来说,我想用一个叫做'commandv'的变量,它存储了在abfdata.sweepC中定义的数组,以下是存储为'get_command-Voltage.py'的python脚本:

import pyabf
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from matplotlib import gridspec
import scipy.io as sio   
import scipy
from scipy import signal
from scipy.signal import find_peaks
from scipy.optimize import curve_fit
filepath1 = ... %full path to an abf file
#load data
def get_command_V(filepath):
    abfdata = pyabf.ABF(filepath)
    print(abfdata) #gives info about abfdata, such as no. of sweeps, channels, duration
    
   
    # print  command input for each sweep
    for i in abfdata.sweepList:
        abfdata.setSweep(i)
        print("sweep command (DAC):", abfdata.sweepC)
    #graph command input data
    figg = plt.figure(figsize=(8, 5))
    plt.title("Command input")
    plt.ylabel(abfdata.sweepLabelY)
    plt.xlabel(abfdata.sweepLabelC)
    for i in abfdata.sweepList:
        abfdata.setSweep(i)
        plt.plot(abfdata.sweepX, abfdata.sweepC, alpha=.5, label="sweep %d" % (i))
    plt.legend()
    plt.show()
    #return command input array and its figure
    sio.savemat('commandv.mat', {'commandv':abfdata.sweepC})
   
    return abfdata.sweepC
commandv = get_command_V(filepath1)

我尝试了以下脚本来获取command_var的内容,并在matlab中绘制图形:

pyenv('Version', '/Users/sayakaminegishi/opt/anaconda3/bin/python');
pyenv

%make this folder searchable by python
if count(py.sys.path,pwd) == 0
    insert(py.sys.path,int32(0),pwd);
end
filepath_this = ... %full path, identical to one defined in the python script

commandv = pyrunfile("get_command_Voltage.py") %get command voltage array

vars = load('commandv.mat');

% Access command_var from the .mat file
command_var = vars.commandv;

figure(1);
plot(command_var) %plot command_var. does not work.

但是它给出的输出是:

>> runningpython
Caught unexpected exception of unknown type.

ans = 

  PythonEnvironment with properties:

          Version: "3.9"
       Executable: "/Users/sayakaminegishi/opt/anaconda3/bin/python"
          Library: "/Users/sayakaminegishi/opt/anaconda3/lib/libpython3.9.dylib"
             Home: "/Users/sayakaminegishi/opt/anaconda3"
           Status: Loaded
    ExecutionMode: InProcess
        ProcessID: "28331"
      ProcessName: "MATLAB"

ABF (v1.649) with 1 channel (pA), sampled at 10.0 kHz, containing 11 sweeps, having no tags, with a total length of 2.06 minutes, recorded with protocol "CaCC_Vclamp_IVsteps_Henckels2020".
sweep command (DAC): [0. 0. 0. ... 0. 0. 0.]
sweep command (DAC): [0. 0. 0. ... 0. 0. 0.]
sweep command (DAC): [0. 0. 0. ... 0. 0. 0.]
sweep command (DAC): [0. 0. 0. ... 0. 0. 0.]
sweep command (DAC): [0. 0. 0. ... 0. 0. 0.]
sweep command (DAC): [0. 0. 0. ... 0. 0. 0.]
sweep command (DAC): [0. 0. 0. ... 0. 0. 0.]
sweep command (DAC): [0. 0. 0. ... 0. 0. 0.]
sweep command (DAC): [0. 0. 0. ... 0. 0. 0.]
sweep command (DAC): [0. 0. 0. ... 0. 0. 0.]
sweep command (DAC): [0. 0. 0. ... 0. 0. 0.]
>> 

而我本来期待的是除了给出abfdata.sweepC的矩阵外,还能得到像这样的图:

enter image description here 如果你能帮我解决这个问题,我将非常感激。任何建议都非常欢迎!

非常感谢你!

0 个回答

暂无回答

撰写回答