Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Matlab to Python
#1
I'm trying to convert the following MATLAB code to Python. So far my attempts have not yielded a positive result.

opts = detectImportOptions('Z_AzimuthSequence.dat');
data = readtable('Z_AzimuthSequence.dat',opts);
tol = 1e-6;
filter = 4.125;
filterreddata = data(abs(data.X - filter) < tol, :);
filteredions = data(ismember(data.IonN, filterreddata.IonN),:);
filteredions(2:2:end,:) = [];
writetable(filteredions, 'Published/test.csv');

This is the problematic python code:

opts = pd.read_csv('Z_AzimuthSequence.dat')
data = opts.readlines ()
Elv = []
KE = []
X = []
filter = 4.125
filterreddata = data(abs(data.X - filter) < tol, [:])
filteredions = data(ismember(data.IonN, filterreddata.IonN),[:])
filteredions(2:2:end,[:]) = []
writetable(filteredions, 'Published/test.csv')

I think the main problem I have is the correct referencing of a string/array in python and finding a suitable equivalent python function to replace the Matlab one.
Reply
#2
First, put your code in Python code tags, you can find help here.

Can you post specifically what a positive result is? And what result/error you are getting instead?
Reply
#3
Post some rows sampledata with headings. I'm not familiar with Matlab.
Maybe it helps other people to understand your problem,
when you explain what your program is doing.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
You need more that just change the input to a pandas DataFrame to convert a matlab script to a python one.
First of all, array indexing in matlab uses the (i, j...) syntax with indexes starting at 1 and python uses [i, j...] with first element 0.
The ismember operation in python is done using sets or list with the syntax "if x in {1, 2, 3}" but in matlab can have several uses and you must see really which is the intended selection.

My recommendation is that you read first the introduction to python and numpy or otherwise your task will be almost impossible...

Otherwise you might want to explore the Matlab to Python conversion using some of the tools of this StackOverflow question or this post or stick to matlab and use octave or scilab.

... and I think I abuse of the links a little bit.
Reply
#5
Thank you all for your responses. I have a large data file (attached) and I'm only interested in rows with specific x values of 4.125 as shown below. Now because the value of 4.125 relates to the stop position of the ion, the corresponding start position is also of interest to me and I want to keep this information in the array. This is a program which effectively finds the x stop position of 4.125 and retains the ion start position. It is a 120982 * 9 array and in the example shown below I would be interested in keeping the information of ion # 3849096.

I keep the start position information of the ion in the array. For each Ion N, there are two instances. The 4.125 filter is useful in letting me know which ion number is of interest to me. However, 4.125 relates to the stop position and what I wish to have is an array with the start position only/ or an option to combine both start and stop. Once I've filtered the data according to the criteria, I get the ion numbers with filtereddata.IonN. These numbers can be used to extract all the matching rows of the original table, using ismember:

"Ion N","Mass","Charge","X","Y","Z","Azm","Elv","KE"

3849094,0.00054858,-1,66.5216,-51,-3.8,-180,88.7,18160

3849094,0.00054858,-1,27.3925,30.3532,-4.07076,-177.1,41.5494,17697.2

3849095,0.00054858,-1,66.5216,-51,-3.7,-180,88.7,18160

3849095,0.00054858,-1,26.6277,31.0039,-3.91402,-177.096,40.8293,17699.4

3849096,0.00054858,-1,66.5216,-51,-3.6,-180,88.7,18160

3849096,0.00054858,-1,4.125,44.9887,-2.47517,-176.363,25.715,17711.1
Reply
#6
A positive result yields the table below:

IonN Mass Charge X Y Z Azm Elv KE
292 0.00054858 -1 49.0803 -55 8.96605 -180 88.1294 8990.28
829 0.00054858 -1 49.928 -55 -9.9565 -180 86.4163 9634.78
940 0.00054858 -1 46.0668 -55 8.97806 -180 89.3835 9434.07
963 0.00054858 -1 45.2893 -55 -1.69968 0 87.4977 8671.51
1779 0.00054858 -1 43.1571 -55 -7.22375 0 86.9737 8374.35
1963 0.00054858 -1 51.4278 -55 -12.175 -180 84.0618 10069.5

As you can see, I now only have one event for each ion number.
Reply
#7
Please let me knoiw if you need the CSV
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem of converting Matlab code to python DongyanZ 2 1,417 Feb-03-2023, 01:04 PM
Last Post: jefsummers
  Convolution "same" in Python doesn't work as Matlab claw91 4 3,768 Oct-01-2020, 08:59 AM
Last Post: claw91
  Python equivalent of Matlab code kwokmaster 5 7,092 Apr-03-2020, 12:02 AM
Last Post: scidam
  Is there similar function to lsqnonlin (in matlab) in python? Jay_Nerella 1 5,963 Nov-11-2019, 08:40 AM
Last Post: feli_x
  How to use .m matlab file in python ? sameer 5 15,396 May-10-2018, 09:39 AM
Last Post: wavic
  Importing matlab cell array (.mat) into a python list scanato 0 8,674 Nov-15-2017, 11:04 AM
Last Post: scanato

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020