1

I have a main python script and I want to call a Matlab function (spm_filter.m) on an array.

I use something like the following:

import matlab.engine
eng = matlab.engine.start_matlab()
y_filtered = np.asarray(eng.spm_filter(filter_kernel, matlab.double(data.tolist()) ,nargout=1))

This works fine but it is extremely slow compared to the case where I use only Matlab to do the same thing e.g. by doing:

y_filtered = spm_filter(filter_kernel, data)

Why is the matlab engine so slow? How can I make it faster?

I do not time the start of the engine. It seems that the slow down is coming due to matlab.double(data.tolist(). Any way to improve this part?

seralouk
  • 30,938
  • 9
  • 118
  • 133
  • What is "extremely slow"? Starting MATLAB will take time, but its a one-off initialization thing – Paolo Nov 01 '21 at 22:47
  • There are two conversions of data here: `matlab.double(data.tolist())` and another one here: `np.asarray(...)`. I would guess most of your slowdown happens there. Unless you include the `matlab.engine.start_matlab()` part in your timings, starting up MATLAB will certainly take time. – Cris Luengo Nov 01 '21 at 22:51
  • @Paolo starting the engine takes 1 sec but executing the command takes minutes whereas through Matlab it takes seconds – seralouk Nov 01 '21 at 22:55
  • @CrisLuengo no I do not include the start of the engine. I am only talking about the execution of the command. It takes minutes whereas through Matlab it takes seconds... – seralouk Nov 01 '21 at 22:55
  • It seems that the slow down is coming due to matlab.double(data.tolist(). Any way to improve this part? – seralouk Nov 01 '21 at 22:58
  • @seralouk 1 sec to open MATLAB? I seriously doubt that; how did you get this measurement? – Paolo Nov 01 '21 at 22:58
  • @Paolo I do not use the interface. I simply use `import matlab.engine eng = matlab.engine.start_matlab()` and this takes 1-2 secs – seralouk Nov 01 '21 at 22:59
  • I've never used MATLAB in Python but it's a huge missed opportunity if it can't handle numpy arrays out of the box. Converting a numpy array to a list and then back to an array in MATLAB seems like a huge, wasteful mess. – Andras Deak -- Слава Україні Nov 01 '21 at 23:02
  • 1
    @AndrasDeak It cannot. You need to do that otherwise it throws an error for the data type. – seralouk Nov 01 '21 at 23:05
  • I've closed as duplicate of a post that I think will solve your problem. Let me know if this is not sufficient. There is also [this post](https://stackoverflow.com/q/34155829/7328782) that has a way to speed up the conversion of MATLAB to NumPy. – Cris Luengo Nov 01 '21 at 23:09
  • @CrisLuengo the first post refers to 1D arrays that are lists already. In my case I have 2D arrays and thus `matlab.double(centered_data_session.tolist())` is inevitable... – seralouk Nov 01 '21 at 23:17
  • That first answer should work for any numeric NumPy array. The 3rd answer provides another solution. – Cris Luengo Nov 01 '21 at 23:47
  • Just to mention and finally close this question that only this (https://stackoverflow.com/a/45284125/5025009) helped in my case. Saving the data in `.mat`format and then calling the matlab function that internally loads the data is extremely fast. I am able to do my task very fast now. – seralouk Nov 02 '21 at 09:02

0 Answers0