Hướng dẫn python pass string to matlab - chuỗi truyền python tới matlab

Nội dung chính

Ví dụ này cho thấy cách sử dụng các biến Python®

exFolders = 
  Python int with properties:

    denominator: [1×1 py.int]
           imag: [1×1 py.int]
      numerator: [1×1 py.int]
           real: [1×1 py.int]

    267

3 trong MATLAB®.

Gọi chức năng Python với các đối số đầu vào exFolders = Python int with properties: denominator: [1×1 py.int] imag: [1×1 py.int] numerator: [1×1 py.int] real: [1×1 py.int] 267 3

Để gọi hàm Python có đối số đầu vào

exFolders = 
  Python int with properties:

    denominator: [1×1 py.int]
           imag: [1×1 py.int]
      numerator: [1×1 py.int]
           real: [1×1 py.int]

    267

3, hãy chuyển chuỗi MATLAB hoặc vectơ ký tự. MATLAB tự động chuyển đổi các giá trị thành loại Python
exFolders = 
  Python int with properties:

    denominator: [1×1 py.int]
           imag: [1×1 py.int]
      numerator: [1×1 py.int]
           real: [1×1 py.int]

    267

3.

Ví dụ: hàm Python

exFolders = 
  Python int with properties:

    denominator: [1×1 py.int]
           imag: [1×1 py.int]
      numerator: [1×1 py.int]
           real: [1×1 py.int]

    267

7 có thông tin về nội dung của thư mục, được chỉ định là loại
exFolders = 
  Python int with properties:

    denominator: [1×1 py.int]
           imag: [1×1 py.int]
      numerator: [1×1 py.int]
           real: [1×1 py.int]

    267

3. Tạo một vectơ ký tự đại diện cho một thư mục hợp lệ và gọi
exFolders = 
  Python int with properties:

    denominator: [1×1 py.int]
           imag: [1×1 py.int]
      numerator: [1×1 py.int]
           real: [1×1 py.int]

    267

7. Số lượng các thư mục ví dụ dựa trên sản phẩm đã cài đặt của bạn.

folder = fullfile(matlabroot,'help','examples');
F = py.os.listdir(folder);
exFolders = py.len(F)

exFolders = 
  Python int with properties:

    denominator: [1×1 py.int]
           imag: [1×1 py.int]
      numerator: [1×1 py.int]
           real: [1×1 py.int]

    267

Sử dụng loại Python exFolders = Python int with properties: denominator: [1×1 py.int] imag: [1×1 py.int] numerator: [1×1 py.int] real: [1×1 py.int] 267 3 trong MATLAB

Trong MATLAB, một chuỗi Python là một biến

p = 
  Python str with no properties.

    ;

1. Để sử dụng biến này trong MATLAB, hãy gọi
p = 
  Python str with no properties.

    ;

2. Ví dụ, hàm Python
p = 
  Python str with no properties.

    ;

3 trả về ký tự phân tách đường dẫn Python, một dấu chấm phẩy (
p = 
  Python str with no properties.

    ;

4).

p = 
  Python str with no properties.

    ;

Để chèn ký tự này giữa các tên đường dẫn, loại:

['mypath' char(p) 'nextpath']

Đọc các phần tử trong chuỗi python

Bạn có thể lập chỉ mục vào một chuỗi python giống như cách bạn lập chỉ mục vào chuỗi MATLAB. Tạo một vectơ ký tự MATLAB và hiển thị một loạt các ký tự.

str = 'myfile';
str(2:end)

Chuyển đổi vectơ ký tự thành loại Python

exFolders = 
  Python int with properties:

    denominator: [1×1 py.int]
           imag: [1×1 py.int]
      numerator: [1×1 py.int]
           real: [1×1 py.int]

    267

3 và hiển thị cùng các ký tự.

pstr = py.str(str);
pstr(2:end)

ans = 
  Python str with no properties.

    yfile

Vượt qua ký tự điều khiển Backslash Matlab

Để vượt qua ký tự điều khiển ngược (

p = 
  Python str with no properties.

    ;

6) dưới dạng loại Python
exFolders = 
  Python int with properties:

    denominator: [1×1 py.int]
           imag: [1×1 py.int]
      numerator: [1×1 py.int]
           real: [1×1 py.int]

    267

3, hãy chèn ký tự điều khiển dòng mới
p = 
  Python str with no properties.

    ;

8 bằng cách gọi hàm MATLAB
p = 
  Python str with no properties.

    ;

9. Python thay thế
p = 
  Python str with no properties.

    ;

8 bằng một dòng mới.

py.str(sprintf('The rain\nin Spain.'))

________số 8

Không có chức năng

p = 
  Python str with no properties.

    ;

9, cả MATLAB và Python diễn giải
p = 
  Python str with no properties.

    ;

6 như một dấu gạch chéo ngược.

py.str('The rain\nin Spain.')

exFolders = 
  Python int with properties:

    denominator: [1×1 py.int]
           imag: [1×1 py.int]
      numerator: [1×1 py.int]
           real: [1×1 py.int]

    267

0

Chuyển chuỗi này sang phương thức chuỗi Python

['mypath' char(p) 'nextpath']
3. Python coi vectơ ký tự MATLAB là một chuỗi thô và thêm ký tự
p = 
  Python str with no properties.

    ;

6 để bảo tồn dấu gạch chéo ngược ban đầu.raw string and adds a
p = 
  Python str with no properties.

    ;

6 character to preserve the original backslash.

exFolders = 
  Python int with properties:

    denominator: [1×1 py.int]
           imag: [1×1 py.int]
      numerator: [1×1 py.int]
           real: [1×1 py.int]

    267

1

exFolders = 
  Python int with properties:

    denominator: [1×1 py.int]
           imag: [1×1 py.int]
      numerator: [1×1 py.int]
           real: [1×1 py.int]

    267

2

Xin chào, đây có thể là một cái gì đó thực sự đơn giản đối với một người đã làm điều này, nhưng tôi đang gặp khó khăn khi chuyển một biến chuỗi cho kịch bản MATLAB từ Python bằng API Matlab Python. Tôi đang làm điều này vào năm 2017b

Đây là những gì tôi có: 1. Hàm MATLAB: config = parseInputs (tên tệp) %fileName là một mảng char trong matlab như "config.csv"

2. Tệp tập lệnh Python cố gắng gọi hàm MATLAB trên. Tôi đã lưu chức năng của mình parseinputs.m trong cùng thư mục với tập lệnh Python.

Dưới đây là kịch bản Python của tôi:

Nhập Matlab.Enginematlab.engine

Eng = matlab.engine.start_matlab ()

fileName = "config.csv""config.csv"

ParameterMy = eng.ParseInputs (tên tệp, nargout = 2)

Tôi đang gặp một lỗi như dưới đây:

Tệp "C: \ ProgramData \ Anaconda3 \ lib \ Site-Packages \ Matlab \ Engine \ fevalfuture.py", dòng 82, kết quả"C:\ProgramData\Anaconda3\lib\site-packages\matlab\engine\fevalfuture.py", line 82, in result

self._result = pythonengine.getfevalresult (self._future, self._nargout, none, out = self._out, err = self._err)_result = pythonengine.getFEvalResult(self._future,self._nargout, None, out=self._out, err=self._err)

MATLABEXECUSTERError: Hàm không xác định 'parseInputs' cho các đối số đầu vào của loại 'char'.

Bất kỳ gợi ý về cách làm cho nó hoạt động? Cảm ơn!

Nội dung chính

Chuyển dữ liệu từ MATLAB đến PythonMATLAB to Python

Khi các hàm MATLAB® trả về các đối số đầu ra, MATLAB chuyển đổi dữ liệu thành các loại dữ liệu Python® tương đương.MATLAB converts the data into equivalent Python® data types.

Loại đối số đầu ra MATLAB (vô hướng trừ khi có ghi chú khác)
(scalar unless otherwise noted)

Kết quả là loại dữ liệu Python

Mảng số

['mypath' char(p) 'nextpath']
5 Đối tượng mảng số (xem mô -đun Matlab Python)

['mypath' char(p) 'nextpath']
6,
['mypath' char(p) 'nextpath']
7

['mypath' char(p) 'nextpath']
8

Phức tạp (bất kỳ loại số)

['mypath' char(p) 'nextpath']
9

str = 'myfile';
str(2:end)
0,
str = 'myfile';
str(2:end)
1,
str = 'myfile';
str(2:end)
2,
str = 'myfile';
str(2:end)
3,
str = 'myfile';
str(2:end)
4

str = 'myfile';
str(2:end)
5

str = 'myfile';
str(2:end)
6,
str = 'myfile';
str(2:end)
7,
str = 'myfile';
str(2:end)
8

str = 'myfile';
str(2:end)
5

pstr = py.str(str);
pstr(2:end)
0

pstr = py.str(str);
pstr(2:end)
1

pstr = py.str(str);
pstr(2:end)
2

pstr = py.str(str);
pstr(2:end)
3

pstr = py.str(str);
pstr(2:end)
4

pstr = py.str(str);
pstr(2:end)
5

p = 
  Python str with no properties.

    ;

2 Mảng (1-BY -____ 57, ________ 57-BY-1)
p = 
  Python str with no properties.

    ;

2 Mảng (________ 60-BY -________ 57)
p = 
  Python str with no properties.

    ;

2 array (
ans = 
  Python str with no properties.

    yfile

0-by-
pstr = py.str(str);
pstr(2:end)
7)

exFolders = 
  Python int with properties:

    denominator: [1×1 py.int]
           imag: [1×1 py.int]
      numerator: [1×1 py.int]
           real: [1×1 py.int]

    267

3 không được hỗ trợ
Not supported

kết cấu

ans = 
  Python str with no properties.

    yfile

3

Hàng hoặc mảng ô cột

ans = 
  Python str with no properties.

    yfile

4

________ 60-BY -________ 57 Mảng ôKhông được hỗ trợ

Đối tượng xử lý MATLAB (chẳng hạn như loại

ans = 
  Python str with no properties.

    yfile

7)

ans = 
  Python str with no properties.

    yfile

8

MATLAB trả về một tham chiếu đến

ans = 
  Python str with no properties.

    yfile

8, không phải là chính đối tượng. Bạn không thể vượt qua
ans = 
  Python str with no properties.

    yfile

8 giữa các phiên MATLAB.

Đối tượng giá trị MATLAB (chẳng hạn như loại

py.str(sprintf('The rain\nin Spain.'))
1)

Đối tượng mờ. Bạn có thể chuyển một đối tượng giá trị cho hàm MATLAB, nhưng bạn không thể tạo hoặc sửa đổi nó.

Đối tượng khác (ví dụ: đối tượng Java®)Không được hỗ trợ

Đối tượng xử lý MATLAB (chẳng hạn như loại

ans = 
  Python str with no properties.

    yfile

7)

MATLAB trả về một tham chiếu đến

ans = 
  Python str with no properties.

    yfile

8, không phải là chính đối tượng. Bạn không thể vượt qua
ans = 
  Python str with no properties.

    yfile

8 giữa các phiên MATLAB.

Đối tượng giá trị MATLAB (chẳng hạn như loại

py.str(sprintf('The rain\nin Spain.'))
1)

MATLAB trả về một tham chiếu đến

ans = 
  Python str with no properties.

    yfile

8, không phải là chính đối tượng. Bạn không thể vượt qua
ans = 
  Python str with no properties.

    yfile

8 giữa các phiên MATLAB.

Đối tượng giá trị MATLAB (chẳng hạn như loại
py.str(sprintf('The rain\nin Spain.'))
1)
Không được hỗ trợ

Đối tượng xử lý MATLAB (chẳng hạn như loại

ans = 
  Python str with no properties.

    yfile

7)

MATLAB trả về một tham chiếu đến

ans = 
  Python str with no properties.

    yfile

8, không phải là chính đối tượng. Bạn không thể vượt qua
ans = 
  Python str with no properties.

    yfile

8 giữa các phiên MATLAB.

Đối tượng giá trị MATLAB (chẳng hạn như loại py.str(sprintf('The rain\nin Spain.'))1)Python to MATLAB

Đối tượng mờ. Bạn có thể chuyển một đối tượng giá trị cho hàm MATLAB, nhưng bạn không thể tạo hoặc sửa đổi nó.MATLAB converts the data into equivalent MATLAB data types.

Đối tượng khác (ví dụ: đối tượng Java®)

Xử lý chức năng
(scalar unless otherwise noted)

['mypath' char(p) 'nextpath']
5 Đối tượng mảng số (xem mô -đun Matlab Python)

Mảng số

['mypath' char(p) 'nextpath']
8

['mypath' char(p) 'nextpath']
6

['mypath' char(p) 'nextpath']
9

['mypath' char(p) 'nextpath']
5 Đối tượng mảng số (xem mô -đun Matlab Python)

str = 'myfile';
str(2:end)
5

['mypath' char(p) 'nextpath']
6,
['mypath' char(p) 'nextpath']
7

Phức tạp (bất kỳ loại số)Mac)

ans = 
  Python str with no properties.

    The rain
    in Spain.

0

pstr = py.str(str);
pstr(2:end)
0

ans = 
  Python str with no properties.

    The rain
    in Spain.

2

pstr = py.str(str);
pstr(2:end)
2

pstr = py.str(str);
pstr(2:end)
5

pstr = py.str(str);
pstr(2:end)
4

exFolders = 
  Python int with properties:

    denominator: [1×1 py.int]
           imag: [1×1 py.int]
      numerator: [1×1 py.int]
           real: [1×1 py.int]

    267

3

p = 
  Python str with no properties.

    ;

2

ans = 
  Python str with no properties.

    The rain
    in Spain.

8

str = 'myfile';
str(2:end)
0,
str = 'myfile';
str(2:end)
1,
str = 'myfile';
str(2:end)
2,
str = 'myfile';
str(2:end)
3,
str = 'myfile';
str(2:end)
4

py.str('The rain\nin Spain.')
0

str = 'myfile';
str(2:end)
0,
str = 'myfile';
str(2:end)
1,
str = 'myfile';
str(2:end)
2,
str = 'myfile';
str(2:end)
3,
str = 'myfile';
str(2:end)
4

ans = 
  Python str with no properties.

    yfile

3

str = 'myfile';
str(2:end)
6,
str = 'myfile';
str(2:end)
7,
str = 'myfile';
str(2:end)
8

ans = 
  Python str with no properties.

    yfile

4

p = 
  Python str with no properties.

    ;

2 Mảng (1-BY -____ 57, ________ 57-BY-1)
p = 
  Python str with no properties.

    ;

2 Mảng (________ 60-BY -________ 57)

py.str('The rain\nin Spain.')
4

p = 
  Python str with no properties.

    ;

2 Mảng (1-BY -____ 57, ________ 57-BY-1)
p = 
  Python str with no properties.

    ;

2 Mảng (________ 60-BY -________ 57)

py.str('The rain\nin Spain.')
5

p = 
  Python str with no properties.

    ;

2 Mảng (1-BY -____ 57, ________ 57-BY-1)
p = 
  Python str with no properties.

    ;

2 Mảng (________ 60-BY -________ 57)

py.str('The rain\nin Spain.')
6

MATLAB trả về một tham chiếu đến

ans = 
  Python str with no properties.

    yfile

8, không phải là chính đối tượng. Bạn không thể vượt qua
ans = 
  Python str with no properties.

    yfile

8 giữa các phiên MATLAB.

py.str('The rain\nin Spain.')
7

p = 
  Python str with no properties.

    ;

2 Mảng (1-BY -____ 57, ________ 57-BY-1)
p = 
  Python str with no properties.

    ;

2 Mảng (________ 60-BY -________ 57)

py.str('The rain\nin Spain.')
8

MATLAB trả về một tham chiếu đến

ans = 
  Python str with no properties.

    yfile

8, không phải là chính đối tượng. Bạn không thể vượt qua
ans = 
  Python str with no properties.

    yfile

8 giữa các phiên MATLAB.

py.str('The rain\nin Spain.')
9.
exFolders = 
  Python int with properties:

    denominator: [1×1 py.int]
           imag: [1×1 py.int]
      numerator: [1×1 py.int]
           real: [1×1 py.int]

    267

00

MATLAB trả về một tham chiếu đến

ans = 
  Python str with no properties.

    yfile

8, không phải là chính đối tượng. Bạn không thể vượt qua
ans = 
  Python str with no properties.

    yfile

8 giữa các phiên MATLAB.

Đối tượng giá trị MATLAB (chẳng hạn như loại py.str(sprintf('The rain\nin Spain.'))1)

  • Đối tượng mờ. Bạn có thể chuyển một đối tượng giá trị cho hàm MATLAB, nhưng bạn không thể tạo hoặc sửa đổi nó.
  • Đối tượng khác (ví dụ: đối tượng Java®)

Làm cách nào để chuyển dữ liệu từ Python sang MATLAB?

Dữ liệu Python có thể được chuyển đổi trở lại thành kiểu dữ liệu MATLAB thông qua chuyển đổi loại, ví dụ:Char (MyPyStr).Hàm này tự động hóa việc xác định kiểu dữ liệu Python và chuyển đổi nó thành kiểu dữ liệu MATLAB thích hợp và ngược lại.through type-conversion, e.g. char(myPyStr). This function automates the identification of the Python data type and convert it to the appropriate MATLAB data type and vice versa.

Python có thể tương tác với Matlab không?

MATLAB cung cấp tích hợp hai chiều linh hoạt với nhiều ngôn ngữ lập trình, bao gồm cả Python.Điều này cho phép các nhóm khác nhau làm việc cùng nhau và sử dụng các thuật toán MATLAB trong các hệ thống phần mềm sản xuất và CNTT.. This allows different teams to work together and use MATLAB algorithms within production software and IT systems.

Làm thế nào để bạn gọi một chức năng MATLAB từ Python?

Có hai cách tiếp cận để gọi mã MATLAB từ Python.Đầu tiên là sử dụng API MATLAB Engine cho Python, yêu cầu cài đặt MATLAB.Thứ hai là sử dụng SDK trình biên dịch MATLAB để biên dịch gói Python không yêu cầu người dùng cài đặt MATLAB.use the MATLAB Engine API for Python, which requires a MATLAB install. The second is to use MATLAB Compiler SDK to compile a Python package that does not require users to have a MATLAB install.