Which is the equivalent function in matlab for bytearray() function in python

9 visualizaciones (últimos 30 días)
Hello,
I'm having troubles trying to convert a python code to matlab.
I have this code, it takes values from the ui and makes a stpack from struct library which packs the values in the way indicated
"<fififfIi"
. The code is shown below.
config_data = bytearray(stpack("<fififfIi", self.sample_freq.get(), int(self.run_time.get()*self.sample_freq.get()), self.bia_freq.get(), self.mode.get(), self.sweep_start.get(), self.sweep_stop.get(), int(np.ceil(self.sweep_points.get())), self.sweep_type.get()))
The result I get, which is correct, is
bytearray(b'050000.00 01006.441895 0000.789513\x00')
I tried to reproduce that in matlab and I was told to use the following code:
config_data = uint8([char("<fififfIi"), 1, 10, 50000, 0, 500, 50000, 10, 0]);
The numbers inside the function are similar to the real ones but it is just to test the output of the code. However when I take a look at the output, this is what I get:
>> NanoBlA4Wire
Columns 1 through 7
60 102 105 102 105 102 102
Columns 8 through 14
73 105 1 10 255 0 255
Columns 15 through 17
255 10 0
I don't understand what the output is or how can I obtain the same output as python in matlab. Does anybody have any idea on what am I missing?
  2 comentarios
Walter Roberson
Walter Roberson el 28 de Abr. de 2021
I am not finding documentation for python stpack() ?
Maria Dolors Farré
Maria Dolors Farré el 28 de Abr. de 2021
Is the pack function from struct module, sorry, inside the code it's renamed from pack to stpack.

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 28 de Abr. de 2021
cd1 = unicode2native(char("<fififfIi"), 'utf-8')
cd1 = 1×9
60 102 105 102 105 102 102 73 105
cd2 = typecast([1, 10, 50000, 0, 500, 50000, 10, 0], 'uint8')
cd2 = 1×64
0 0 0 0 0 0 240 63 0 0 0 0 0 0 36 64 0 0 0 0 0 106 232 64 0 0 0 0 0 0
config_data = [cd1, cd2]
config_data = 1×73
60 102 105 102 105 102 102 73 105 0 0 0 0 0 0 240 63 0 0 0 0 0 0 36 64 0 0 0 0 0
You have a bit of a logicistics problem that your character string is not fixed length, so if you were trying to convert back you would not know when to stop converting the bytes into characters.
char(config_data)
ans = '<fififfIi ð? $@ jè@ @@ jè@ $@ '

Categorías

Más información sobre Call Python from MATLAB en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by