Which is the equivalent function in matlab for bytearray() function in python
9 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Walter Roberson
2021-4-28
cd1 = unicode2native(char("<fififfIi"), 'utf-8')
cd2 = typecast([1, 10, 50000, 0, 500, 50000, 10, 0], 'uint8')
config_data = [cd1, cd2]
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)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Call Python from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!