loading existing variables from a python script in matlab

26 次查看(过去 30 天)
Hi, I am just wondering how to use data from an existing python variable (defined in a .py script) in matlab.
Specifically, I would like to use the variable 'commandv' that stores an array defined in abfdata.sweepC, given the following script in python (stored as 'get_command-Voltage.py':
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)
I tried the following script in matlab but it didn't work:
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.
It would be so appreciated if you could help me resolve this issue and get the contents of command_var and a plot of it in matlab.
Any advice would be greatly appreciated!
Thank you so much.

回答(1 个)

Poorna
Poorna 2024-3-31
Hi Sayaka,
I see you want to get access to the variable "commandv" in the python script in MATLAB. You can use the "pyrunfile" function to execute the python script and get the desired variables as output of that execution. When you use "pyrunfile", you can directly indicate the variables you're interested in by listing them as part of the function call. This approach ensures that after the execution of the Python script, the specified variables are readily available in MATLAB.
In your case, you can modify the function call to the "pyrunfile" as below to get the "commandv" variable
commandv = pyrunfile("get_command_Voltage.py", 'commandv')
To know more about "pyrunfile", refer to the following documentation.
Hope this Helps!

类别

Help CenterFile Exchange 中查找有关 Call Python from MATLAB 的更多信息

产品


版本

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by