Results returned from Matlab Engine API is different than Matlab results

Hi.
I am running Matlab R2022b verison. I have installed the Matlab Engine API as well on Python 3.9.7.
I am having a problem with the disperency of the results returned from both. The original code is written in Matlab, and I want to run it using python. When I run Matlab, the output is a variable with 1x18 shape, which values are:
6.14395465881702 0.651575092548687 25 156.250000000000 390.625000000000
0.770530090496081 0.192234457190110 5.32927289961177e-11 1.35764820806248e-10 11.2398708837811
0.0290391963859361 0.0290391963859361 -10.4903501698474 0.388954806990836 0.520706129341726
0.0693651908321199 0.0209738728353181 0.0183603630161085
When running the same code using Matlab Engine API on python, the output is:
6.105234670562155 0.644549638182511 25. 0. 0.
0.784439528813156 0.194095308440285 0.000000000054579 0.000000000137327 10.462903307448205
0.028765657088391 0.028765657088391 -10.510744455041587 0.400946789499116 0.510874395438037
0.067597599702771 0.020581215360076 0.018303702998913
Most out of the outputs are very close to the orignal ones using Matlab, but some variables are completely zeroed, and I noticed these are the values are considered very large in comparison to the output of the other values. Is there a way to fix this?
Thank you.
A small snippit of the python code:
import matlab.engine
matlab = matlab.engine.start_matlab()
path = '...'
matlab.addpath(path)
fs = ..
feats = matlab.compute_feats(data, fs)
matlab.quit()

2 Comments

What is the basic mathematics beyind the calculations?
And are you doing an optimization of some kind?
I am running a very long code. It basically extracts 18 different features from time series data. You can have a glance about the file I am running here: Github Link
I provide some time-series data, sampling frequency, and high-pass filter coefficients in the original function.
function feats=feats_sda(epoch,fs,hpf)
The high-pass filter data (hpf) is not needed anyway for the calculations I want, so I edited the matlab function and removed it. (I only need from lines 1 to 101, the rest is discarded). I have also edited some lines in other places to fix some errors when running using matlab engine. In the end, the files that are used for running are both the same when ran through Matlab itself or the API.
Anyway, the function returns the same result with or without providing a valid high-pass filter for the 18 features I want.
I can provide the epoch.mat and hpf.mat files for replication if you want, as well as the python snippit.

Sign in to comment.

Answers (1)

The problem has been solved. The original code has this lines:
[~, f1(2)] = regularity(snleo,win,olap);
Modifiying the statement to following solved the problem for both f1(3) and f1(4) which originally had the problem.
[~, reg] = regularity(snleo,win,olap);
f1(2) = reg;
Perhaps it is some kind of limitation. I am not sure.

Categories

Products

Release

R2022b

Asked:

on 13 Aug 2023

Edited:

on 13 Aug 2023

Community Treasure Hunt

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

Start Hunting!