I have a script in Matlab, which accesses a DLL and allows me to utilize the methods to import and analyse data programmatically. However, I would like to convert it to python. I have looked at using pythonnet, but I cannot get it to work. Can anyone suggest a way of replicating this behaviour in python?
Ths example is specific to Delsys and their EMGWorks software.
%locate HPF DLL within EMGworks install folder
path = ['C:\Program Files (x86)\pathtoDLL\HPF.dll'];
%make HPF assembly visible to MATLAB
NET.addAssembly(path);
%locate target HPF file
curFile = 'C:\TestFiles\MyTestFile.hpf';
%construct HPF reader
myHPFreader = HPF.HPFReader(curFile);
%invoke “GetAllSampleRates” method on HPF reader object “myHPFreader”
mySampleRates = myHPFreader.GetAllSampleRates;
Thanks Tim!
I made one small change to the great example you provided!
channel_names = emg_file.GetAllChannelNames()
The emg_file change matched the variable.
The key it seems is knowing the structure of the dll so you can instantiate the class. I looked at the Matlab docs to work this out.
1. Install Python.net
pip install pythonnet
2. Import package and make package available to Python
import clr
clr.AddReference('C:\Program Files (x86)\Delsys, Inc\EMGworks\Matlab Conversion Library \HPF.dll')
3. Instantiate class object
This was initially confusing for me. But it seems that the name of the DLL will be the name of the package. In my case the .dll is HPF.dll so my package name is HPF.
from HPF import HPFReader
emg_file = HPFReader(<filename>)
4. Utilise methods from table in matlab docs to get file info and print it out
channel_names = data.GetAllChannelNames()
for channel in channel_names:
print(channel)
Related
I just put some of my code from a/b.dart to a/b1.dart file and now I started getting lot of errors on importing.
Is there any command or any other fix to import all a/b1.dart file in these files instead of manually opening each file and importing one by one.
I understand that a function or a property can be defined in more than two files and Dart can't make the right choice but if a function or property is defined in just one place, I think there must be some way to import it except searching for a/b.dart and replacing it with a/b.dart + a/b1.dart and then optimizing all imports.
As much as I am aware, Plugins/Extensions for your specific IDE (for dart) can be found that will help you with this problem.
I would recommend using dartdev tools - dartfix
I want to convert a STEP file into an STL file format using Python. I have looked online and it looks like the best option is to either use FreeCAD or OpenCascade (OCC). However, I am a beginner and do not know where to start from. I did some search online and found this out (a code to convert STEP to OBJ file).
Are there any python examples from FreeCAD (based on OCC) to convert STEP files to STL? Where should I start?
Here's a quick bit of code to start out:
import FreeCAD
import Part
import Mesh
shape = Part.Shape()
shape.read('my_shape.step')
doc = App.newDocument('Doc')
pf = doc.addObject("Part::Feature","MyShape")
pf.Shape = shape
Mesh.export([pf], 'my_shape.stl')
FreeCAD uses python extensively for user-facing functions. Basically, anything you do through the UI is done with python.
So it's useful to open up the UI, open up the Python console, and then do a function manually. You can often just copy the python directly from the console and edited it to serve your needs.
I'm trying to run these from Karl Skretting for dictionary learning in MATLAB. It is mentioned that mpv2 java package is needed to run these scripts for sparse approximation. where can I find this package and how can i install it so that MATLAB can use its method?
Thanks for your help.
You can find it here:
http://www.ux.uis.no/~karlsk/dle/mpv2-class.zip
Extract all files to folder named 'mpv2' (so folder structure is: C:\Some\Folder\mpv2)
javaaddpath('C:\Some\Folder\')
And to test for success:
exist('mpv2.SimpleMatrix') %should return 8 indicating class.
As a follow up to my previous question, I run into a new obstacle: how to generate stubs for functions in a toolbox?
I found Andy Campbell's solution for the non toolbox case. This does not work in my case because Matlab complains: package directories are not allowed in MATLAB path in Pathfixtures!
I also don't see how this concept will overwrite the import statements within the toolbox, e.g. in file2.
This is my setup:
+folder1/file1.m
+folder1/runtestsuite.m
+folder1/unittest_data/file1_testdata.mat
+folder1/+folder2/file2.m
+folder1/+folder2/unittest_data/overloads/file1.m
...
Let's say I want to stub file1 in file2. And file2 has as a first statement: import folder1.file1.
With
methods(Access=private)
function inject_file1_stub(testCase, answer)
import matlab.unittest.fixtures.PathFixture;
testCase.applyFixture(PathFixture(fullfile(testCase.path,'overloads')));
file1('', answer);
end
end
So currently I believe this concept is not applicable in my case, so how is this done correctly with matlab?
I know one can shadow an implementation of a function in a toolbox, if one adds another path with the same toolboxname and function to the path. For this I would have to recreate a subset of the current folder setup:
So my current idea for a fixture is
create temporary folder with tempdir
use mfilename to check what subset of the toolbox directories I have to recreate
generate folder structure
copy from the overload folder to the new toolbox system
Add this to path
Run tests
In teardown
remove the temporary folder
remove the entry from path
I have not implemented this yet, and seems a bit redundant knowing that there is a Pathfixture in matlab already.
Pointers to other toolboxes which show how they have solved these kind of problems are also welcome.
It is true that you can't add subfolders of packages to the path, but that doesn't mean you can't shadow these path functions. To do this you need to separate the test related content out of your source location. For example, if your source looks like:
<source-home>/+folder1/file1.m
<source-home>/+folder1/+folder2/file2.m
Then you can put your tests somewhere else so your structure would look something like:
<test-home>/file1Test.m
<test-home>/file2Test.m % could also put tests into packages if you want
<test-home>/overloads/+folder1/file1.m
<test-home>/overloads/+folder1/+folder2/file2.m
Then inside of file1Test and/or file2Test you would use a PathFixture to add:
<test-home>/overloads/
to the path.
Also, another thing to consider is defining an interface in your source code for these dependencies and leveraging dependency injection (with or without a DI framework) in order to get test specific behavior into your tests.
I come with the background in languages like Java or Python where modular programming is enabled by packaging system and import directive (aka namespace aliasing). Historically MATLAB's approach to resolve problems like naming conflicts boils down to setting/playing with MATLABPATH, renaming/extending identifiers with prefixes, etc. So far I have been successfully playing with native MATLAB packaging by prepending plus sign "+" before the folder name (MATLAB notation for package also see here). Obviously they are very long to type ;-) Basically I am back to the similar problem as discussed here with no solution. So let me paraphrased for my particular angle:
Assume I have folder +mypackage defined containing file myfun.m with the function code of the same name.
How to achieve aliasing for MATLAB function inside the user (non-java) package as illustrated by the following python code:
from mypackage import myfun
?
[EDIT] Please note that AFAIK import keyword works only for java classes (with jvm attached to MATLAB process). No, import is working perfectly fine for both functions and aliases for objects and function of both Java and MATLAB origin.
Possibly related but not the same.
[EDIT2]
python's
from mypackage import myfun as anotherfun
is equivalent to MATLAB's
anotherfun = #mypackage.myfun
Doesn't
import mypackage.myfun
work?
link to documentation