How can I run python scripts that depend on imported libraries using matlab?

28 次查看(过去 30 天)
Hi, I have a python script that depends on imported packages, as shown:
#script stored as get_command_Voltage.py
import pyabf
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
#load data
def get_command_V(filepath):
abfdata = pyabf.ABF(filepath)
return abfdata
commandv = get_command_V(filepath1)
and I'd like to store the python output, commandv, in the variable commandV in matlab.
I wrote the following code on Matlab (script called vclampanal.m):
pyenv
clear classes
setenv("PYTHONPATH", "/usr/local/bin/python3")
%make this folder searchable by python
if count(py.sys.path,pwd) == 0
insert(py.sys.path,int32(0),pwd);
end
pyabf = py.importlib.import_module('pyabf');
filepath_this = "..." %full file path
commandV = pyrunfile("get_command_Voltage.py","commandv",filepath1 = filepath_this)
but it gives me the error:
Warning: Unable to load Python object. Saving (serializing) Python objects into a MAT-file
is not supported.
> In vclampanal (line 35)
Error using <frozen importlib>_find_and_load_unlocked (line 1140)
Python Error: ModuleNotFoundError: No module named 'pyabf'
Error in <frozen importlib>_find_and_load (line 1176)
Error in <frozen importlib>_gcd_import (line 1204)
Error in __init__>import_module (line 126)
>>
I'm just wondering how I could resolve this issue and run python scripts that depend on imports from python libraries (which were installed using pip).
Any advice would be greatly appreciated!
Thank you

回答(1 个)

Nicholas Zhou
Nicholas Zhou 2024-3-29
you don't need to import anything before calling pyrunfile.
Once you have the get_command_voltage.py ready, this python file does import everything needed, so you just simply call pyrunfile directly as below (which I tried it on Windows with MATLAB R2024a and Python 3.10.4):
>> filepath_this = "130618-1-12.abf"
filepath_this =
"130618-1-12.abf"
>> commandV = pyrunfile("get_command_Voltage.py","commandv",filepath1 = filepath_this)
commandV =
Python ABF - 属性:
fileGUID: [1×36 py.str]
fileUUID: [1×36 py.str]
headerHTML: [1×16209 py.str]
headerMarkdown: [1×9706 py.str]
headerText: [1×9570 py.str]
md5: [1×32 py.str]
sampleRate: [1×1 py.int]
sweepC: [1×1 py.numpy.ndarray]
sweepDerivative: [1×1 py.numpy.ndarray]
sweepTimesMin: [1×1 py.numpy.ndarray]
sweepTimesSec: [1×1 py.numpy.ndarray]
sweepY: [1×1 py.numpy.ndarray]
channelList: [1×1 py.list]
sweepUnitsY: [1×2 py.str]
sweepPointCount: [1×1 py.int]
dataPointByteSize: [1×1 py.int]
dataLengthSec: 4
holdingCommand: [1×20 py.list]
abfVersion: [1×1 py.dict]
abfID: [1×11 py.str]
data: [1×1 py.numpy.ndarray]
abfDateTimeString: [1×23 py.str]
abfFolderPath: [1×16 py.str]
userListParamToVaryName: [1×0 py.list]
userListParamToVary: [1×0 py.list]
dataSecPerPoint: 2.0000e-05
nOperationMode: [1×1 py.int]
channelCount: [1×1 py.int]
sweepEpochs: [1×1 py.pyabf.waveform.EpochSweepWaveform]
sweepChannel: [1×1 py.int]
sweepX: [1×1 py.numpy.ndarray]
dataByteStart: [1×1 py.int]
tagTimesSec: [1×0 py.list]
tagComments: [1×0 py.list]
dataLengthMin: 0.0667
dacUnits: [1×4 py.list]
stimulusByChannel: [1×1 py.list]
sweepList: [1×3 py.list]
abfDateTime: 1806-01-08 17:34:27
sweepIntervalSec: 1
dataPointsPerMs: [1×1 py.int]
userListRepeat: [1×0 py.list]
tagTimesMin: [1×0 py.list]
userListEnable: [1×0 py.list]
sweepNumber: [1×1 py.int]
protocolPath: [1×4 py.str]
userList: [1×0 py.list]
abfFilePath: [1×32 py.str]
sweepLabelX: [1×14 py.str]
adcNames: [1×1 py.list]
creatorVersion: [1×1 py.dict]
dataPointCount: [1×1 py.int]
adcUnits: [1×1 py.list]
sweepUnitsC: [1×2 py.str]
dacNames: [1×4 py.list]
sweepUnitsX: [1×3 py.str]
dataRate: [1×1 py.int]
creatorVersionString: [1×19 py.str]
creator: [1×36 py.str]
stimulusFileFolder: [1×16 py.str]
protocol: [1×4 py.str]
sweepLengthSec: 1
abfVersionString: [1×7 py.str]
sweepLabelC: [1×23 py.str]
tagSweeps: [1×0 py.list]
sweepCount: [1×1 py.int]
sweepLabelD: [1×18 py.str]
abfFileComment: [1×2 py.str]
sweepLabelY: [1×18 py.str]
ABF (v1.299) with 1 channel (pA), sampled at 50.0 kHz, containing 3 sweeps, having no tags, with a total length of 0.07 minutes, recorded without a protocol file.
>>

类别

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