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 to get the contents of command_var and a plot of in 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.
but it gave the output:
>> 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.]
>>
while I was expecting a plot that looks like this in addition to giving a matrix for abfdata.sweepC:
It would be so appreciated if you could help me resolve this issue. Any advice would be greatly appreciated!
Thank you so much