0

I'm trying to call a MATLAB function from python and continue with it's result in python. My script is as simple as this:

import librosa
import numpy as np
import matlab
import matlab.engine

eng = matlab.engine.start_matlab()
eng.addpath('TFDs/', '-end')

audio = librosa.chirp(0, 5000, duration=1, linear=True)
audio = matlab.double(audio.tolist())

choi_williams = eng.dtfd_nonsep(audio ,'cw',{30})
print(choi_williams)

I'm creating a linear chirp and expect to calculate it's time frequency distribution with the help of the fast_TFDs libary. The fast_TFDs folder is part of the current directory I'm executing the script from. But I'm recieving this error message:

Undefined function 'dtfd_nonsep' for input arguments of type 'cell'.

Traceback (most recent call last): File "cw.py", line 12, in choi_williams = eng.dtfd_nonsep(audio ,'cw',{30}) File "/home/dunkeljo/tmp/anaconda3/envs/keras-gpu/lib/python3.8/site-packages/matlab/engine/matlabengine.py", line 70, in call return FutureResult(self._engine(), future, nargs, _stdout, File "/home/dunkeljo/tmp/anaconda3/envs/keras-gpu/lib/python3.8/site-packages/matlab/engine/futureresult.py", line 67, in result return self.__future.result(timeout) File "/home/dunkeljo/tmp/anaconda3/envs/keras-gpu/lib/python3.8/site-packages/matlab/engine/fevalfuture.py", line 82, in result self._result = pythonengine.getFEvalResult(self._future,self._nargout, None, out=self._out, err=self._err) matlab.engine.MatlabExecutionError: Undefined function 'dtfd_nonsep' for input arguments of type 'cell'.#

So what am I doing wrong? I'm not even sure whether the issue is the input arguments or that the function cannot be found. Is the use of the function wrong? Maybe even the import of them? Or is there a problem with the input parameters? Anyone got a clue?

2
  • Does this answer your question? Calling Matlab function from python Commented Mar 8, 2021 at 17:05
  • 1
    The problem is the argument format. The matlab function is expecting a array but the input from python is a cell. I don't have the data but you need to find a way to cast the 'audio' argument properly to a matlab array, with, e.g. cell2mat or similar functions. Commented Mar 9, 2021 at 0:52

1 Answer 1

0

The solution was as wwweagle suggested: Simply add cell2mat an it works:

import librosa
import numpy as np
import matlab
import matlab.engine

eng = matlab.engine.start_matlab()
eng.addpath('TFDs/', '-end')

audio = librosa.chirp(0, 5000, duration=1, linear=True)
audio = eng.cell2mat(audio.tolist())
audio = eng.double(audio)

choi_williams = eng.dtfd_nonsep(audio, 'cw', {eng.double(30)})

print(choi_williams)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.