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

10 ビュー (過去 30 日間)
Maria Dolors Farré
Maria Dolors Farré 2021 年 4 月 28 日
回答済み: Walter Roberson 2021 年 4 月 28 日
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 件のコメント
Walter Roberson
Walter Roberson 2021 年 4 月 28 日
I am not finding documentation for python stpack() ?
Maria Dolors Farré
Maria Dolors Farré 2021 年 4 月 28 日
Is the pack function from struct module, sorry, inside the code it's renamed from pack to stpack.

サインインしてコメントする。

回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 4 月 28 日
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è@ $@ '

カテゴリ

Help Center および File ExchangeCall Python from MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by