Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
MATLAB to Python conversion
#5
I believe the input file is a binary file, with only 32-bit unsigned integers. (I am new to Matlab so don't be cross with me if I am wrong.)
There are several ways to store the data. I would advise to create a 3-dimensional dictionary. But one can argue about that. Perhaps creating a class for storing the data is even more elegant.
As far as I understand your Matlab code I believe you could write it in this way in Python.
import struct                                      # for reading binary data
files = ['C:\temp\day1.dat','C:\temp\day2.dat'];   # Files to read (1 file for each day apple pickers worked)
filedata = {}                                      # prepare empty dictionary for filedata
for file in files:                                 # Loop over each file
   filedata[file] = {}                             # Create new dict for this file
   fid = open(file,'rb')                           # Open for reading binary data
   numShifts = struct.unpack('i', fid.read(4))     # Read # of worker shifts on that day
   for shift in range(1, numShifts):               # Now loop over the shifts
      filedata[file][shift] = {}                   # Create new dict for this shift
      numFarmers = struct.unpack('i', fid.read(4)) # Read number of farmers working that shift
      for farmer in range(1, numFarmers):          # Now read number of apples picked by each farmer on that shift.
         filedata[file][shift][farmer] = struct.unpack('i', fid.read(4))
   close(fid)
I have no data to test it so there may be errors in it. In this case the numShifts and numFarmers are not stored in the 3-D dictionary but I believe you only need them in the process.
I hope I understood your problem right.
Reply


Messages In This Thread
MATLAB to Python conversion - by stokd - Jan-18-2020, 02:24 AM
RE: MATLAB to Python conversion - by stokd - Jan-18-2020, 03:34 AM
RE: MATLAB to Python conversion - by perfringo - Jan-18-2020, 06:37 AM
RE: MATLAB to Python conversion - by buran - Jan-18-2020, 07:05 AM
RE: MATLAB to Python conversion - by ibreeden - Jan-18-2020, 11:11 AM
RE: MATLAB to Python conversion - by stokd - Jan-18-2020, 06:14 PM
RE: MATLAB to Python conversion - by stokd - Jan-18-2020, 08:04 PM
RE: MATLAB to Python conversion - by buran - Jan-18-2020, 08:28 PM
RE: MATLAB to Python conversion - by stokd - Jan-18-2020, 08:32 PM
RE: MATLAB to Python conversion - by ibreeden - Jan-19-2020, 10:00 AM
RE: MATLAB to Python conversion - by stokd - Jan-19-2020, 09:14 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Media Pipe Python Interfacing with MATLAB cmcreecc 1 161 May-30-2024, 07:23 AM
Last Post: TrentErnser
  I need to add data types to cython conversion python to c Good_AI_User 1 1,087 Aug-19-2022, 07:52 AM
Last Post: Gribouillis
Photo Matlab to Python Sateesh 0 1,740 Nov-04-2020, 09:11 AM
Last Post: Sateesh
  From Matlab to Python erbab 1 2,083 Oct-27-2020, 02:16 PM
Last Post: jefsummers
  Arrays in MATLAB and PYTHON cokhuatlanyeuthuongconhetmuc 2 2,274 Jul-24-2020, 10:47 AM
Last Post: cokhuatlanyeuthuongconhetmuc
  Conversion of Oracle PL/SQL(packages, functions, procedures) to python modules. DivyaKumar 2 6,653 Jul-09-2020, 04:46 PM
Last Post: srikanth7482
  Matlab to Python -- Parallel Computing zistambo 1 2,045 Jun-10-2020, 04:59 PM
Last Post: pyzyx3qwerty
  C to Python code conversion print problem anakk1n 1 2,253 May-22-2020, 04:15 PM
Last Post: deanhystad
  Python uppercase conversion conditions Jaypeng 7 3,118 Apr-29-2020, 11:24 AM
Last Post: jefsummers
  Python v MatLab for graphs and plots CynthiaMoore 4 3,158 Apr-22-2020, 02:13 PM
Last Post: CynthiaMoore

Forum Jump:

User Panel Messages

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