Nội dung chính
Ví dụ này cho thấy cách sử dụng các biến từ điển Python® [
studentID = Python dict with no properties. {'Robert': 357.0, 'Mary': 229.0, 'Jack': 391.0}2] trong MATLAB®.
Để gọi hàm Python có đối số đầu vào
studentID = Python dict with no properties. {'Robert': 357.0, 'Mary': 229.0, 'Jack': 391.0}2, hãy tạo biến
studentID = Python dict with no properties. {'Robert': 357.0, 'Mary': 229.0, 'Jack': 391.0}4. Để chuyển đổi
studentID = Python dict with no properties. {'Robert': 357.0, 'Mary': 229.0, 'Jack': 391.0}2 thành biến MATLAB, hãy gọi hàm
studentID = Python dict with no properties. {'Robert': 357.0, 'Mary': 229.0, 'Jack': 391.0}6.
Tạo biến Python studentID =
Python dict with no properties.
{'Robert': 357.0, 'Mary': 229.0, 'Jack': 391.0}
2
Tạo một biến
studentID = Python dict with no properties. {'Robert': 357.0, 'Mary': 229.0, 'Jack': 391.0}2 để chuyển đến hàm Python. Hàm
studentID = Python dict with no properties. {'Robert': 357.0, 'Mary': 229.0, 'Jack': 391.0}9 tạo ra các đối số từ khóa.
studentID = py.dict[pyargs['Robert',357,'Mary',229,'Jack',391]]
studentID = Python dict with no properties. {'Robert': 357.0, 'Mary': 229.0, 'Jack': 391.0}
Ngoài ra, tạo cấu trúc MATLAB và chuyển đổi nó thành biến
studentID = Python dict with no properties. {'Robert': 357.0, 'Mary': 229.0, 'Jack': 391.0}2.
S = struct['Robert',357,'Mary',229,'Jack',391]; studentID = py.dict[S]
studentID = Python dict with no properties. {'Robert': 357.0, 'Mary': 229.0, 'Jack': 391.0}
Sử dụng loại Python studentID =
Python dict with no properties.
{'Robert': 357.0, 'Mary': 229.0, 'Jack': 391.0}
2 trong MATLAB
Để chuyển đổi loại
studentID = Python dict with no properties. {'Robert': 357.0, 'Mary': 229.0, 'Jack': 391.0}2 được trả về từ hàm Python sang biến MATLAB, hãy gọi
studentID = Python dict with no properties. {'Robert': 357.0, 'Mary': 229.0, 'Jack': 391.0}6.
Giả sử bạn có chức năng Python trả về các mục menu và giá trong một đối tượng
studentID = Python dict with no properties. {'Robert': 357.0, 'Mary': 229.0, 'Jack': 391.0}2 có tên
S = struct['Robert',357,'Mary',229,'Jack',391]; studentID = py.dict[S]5. Để chạy mã này trong MATLAB, hãy tạo biến này.
order = py.dict[pyargs['soup',3.57,'bread',2.29,'bacon',3.91,'salad',5.00]]
order = Python dict with no properties. {'soup': 3.57, 'bread': 2.29, 'bacon': 3.91, 'salad': 5.0}
Chuyển đổi
S = struct['Robert',357,'Mary',229,'Jack',391]; studentID = py.dict[S]5 thành biến MATLAB.
myOrder = struct with fields:
soup: 3.5700
bread: 2.2900
bacon: 3.9100
salad: 5
Hiển thị giá của thịt xông khói bằng cú pháp MATLAB.
Hiển thị giá của thịt xông khói bằng cú pháp Python. Loại biến
S = struct['Robert',357,'Mary',229,'Jack',391]; studentID = py.dict[S]7 là gấp đôi, bạn có thể sử dụng trong MATLAB.
Một từ điển có các cặp khóa và giá trị. Hiển thị các mục menu trong biến
S = struct['Robert',357,'Mary',229,'Jack',391]; studentID = py.dict[S]5 bằng hàm Python
S = struct['Robert',357,'Mary',229,'Jack',391]; studentID = py.dict[S]9.
ans = Python dict_keys with no properties. dict_keys[['soup', 'bread', 'bacon', 'salad']]
Hiển thị tất cả giá bằng cách sử dụng hàm Python
studentID = Python dict with no properties. {'Robert': 357.0, 'Mary': 229.0, 'Jack': 391.0}0.
ans = Python dict_values with no properties. dict_values[[3.57, 2.29, 3.91, 5.0]]
Truyền đối số studentID =
Python dict with no properties.
{'Robert': 357.0, 'Mary': 229.0, 'Jack': 391.0}
2 cho phương pháp Python
Lớp Python
studentID = Python dict with no properties. {'Robert': 357.0, 'Mary': 229.0, 'Jack': 391.0}2 có phương pháp
studentID = Python dict with no properties. {'Robert': 357.0, 'Mary': 229.0, 'Jack': 391.0}3. Để chạy mã này, hãy tạo một biến
studentID = Python dict with no properties. {'Robert': 357.0, 'Mary': 229.0, 'Jack': 391.0}2 của bệnh nhân và kết quả xét nghiệm.
patient = py.dict[pyargs['name', 'John Doe', ... 'test1', [], ... 'test2', [220.0, 210.0, 205.0], ... 'test3', [180.0, 178.0, 177.5]]];
Chuyển đổi tên bệnh nhân thành chuỗi MATLAB.
Cập nhật và hiển thị kết quả cho
studentID = Python dict with no properties. {'Robert': 357.0, 'Mary': 229.0, 'Jack': 391.0}5 bằng phương pháp
studentID = Python dict with no properties. {'Robert': 357.0, 'Mary': 229.0, 'Jack': 391.0}3.
studentID = Python dict with no properties. {'Robert': 357.0, 'Mary': 229.0, 'Jack': 391.0}0
studentID = Python dict with no properties. {'Robert': 357.0, 'Mary': 229.0, 'Jack': 391.0}1
- Phần mềm dùng thử
- Phần mềm dùng thử
- Cập nhật sản phẩm
- Cập nhật sản phẩm
Bạn có chia sẻ thêm chi tiết về cách tạo tệp "lib.mat" không? Tôi đã thử các bước sau:
>>lib.name='hello''hello'
>>Strs[1].id=1
>>Strs[2].id=2
>>Strs[3].id=3
>> lib.strs = strs
>> lib.strlist = {1,2,3}
>> lưu ['lib', 'lib']]'lib', 'lib']
Đây là những gì tôi thấy trong Python:
>>> obj = eng.load ['lib.mat'] ['lib']]'lib.mat']['lib']
...
ValueError: Chỉ có thể trả lại một cấu trúc vô hướng từ MATLABscalar struct can be returned from MATLAB
Đây là hành vi dự kiến vì mảng struct không được hỗ trợ theo tài liệu:
//www.mathworks.com/help/matlab/matlab_external/handle-data-returned-from-matlab-to-python.html
Trong trường hợp này, bản thân "lib" là một cấu trúc vô hướng nhưng phần tử "STRS" là một mảng cấu trúc.