Skip to content
MathWorks - Mobile View
  • MathWorks 계정에 로그인합니다.MathWorks 계정에 로그인합니다.
  • Access your MathWorks Account
    • 내 계정
    • 나의 커뮤니티 프로필
    • 라이선스를 계정에 연결
    • 로그아웃
  • 제품
  • 솔루션
  • 아카데미아
  • 지원
  • 커뮤니티
  • 이벤트
  • MATLAB 받기
MathWorks
  • 제품
  • 솔루션
  • 아카데미아
  • 지원
  • 커뮤니티
  • 이벤트
  • MATLAB 받기
  • MathWorks 계정에 로그인합니다.MathWorks 계정에 로그인합니다.
  • Access your MathWorks Account
    • 내 계정
    • 나의 커뮤니티 프로필
    • 라이선스를 계정에 연결
    • 로그아웃

비디오 및 웨비나

  • MathWorks
  • 비디오
  • 비디오 홈
  • 검색
  • 비디오 홈
  • 검색
  • 영업 담당 문의
  • 평가판 신청
3:04 Video length is 3:04.
  • Description
  • Full Transcript
  • Related Resources

How to Call Python from MATLAB

This video shows how to call Python® code from MATLAB® using a sentiment analysis example. This example consists of listening to audio through a microphone, detecting text from speech, and using a pretrained machine learning model to predict the sentiment (positive, negative, or neutral) of the detected text. In the example, MATLAB is first used to record audio data from a microphone. A user-defined Python module is then used to detect text from the audio signal. This text is then returned to MATLAB to continue performing the sentiment analysis.

In this video, you will learn how to call Python code from MATLAB.

To do this, we will use a sentiment analysis example. Suppose a person says that was the best concert they ever attended. A sentiment analysis algorithm will look at this text and output what it thinks is the intent – or the sentiment – of the person. In this case, it should predict that this is a positive statement. On the other hand, if the person says they dropped their ice cream on the floor and they are sad, then the algorithm should predict a negative sentiment.

The sentiment analysis program might look like this. We listen to an audio source like a microphone, detect text from the audio signal, and then classify the text using our sentiment analysis model. Suppose I am doing all my development in MATLAB, but my colleague already has Python code to perform the speech-to-text conversion. Instead of rewriting code or finding a new solution, I want to still use the Python code for speech-to-text and do the rest of my work in MATLAB.

Let’s first see our Python code. I have a Python module that uses the SpeechRecognition package, and particularly the PocketSphinx software from Carnegie Mellon University, to recognize text from speech. In this package, my colleague provided an “audioToText” function. This accepts an audio signal, along with its sampling frequency and channel width, and returns the detected text and a Boolean flag indicating whether the detection was successful.

Going to MATLAB, here is our Live Script showing the entire sentiment analysis process.

First, I will use the “pyversion” function to see which version of the Python interpreter is being picked up by MATLAB. For me, it’s picking up Python 3.7. Refer to the documentation to see which versions are supported for your release of MATLAB, and how to set additional options. In this step, I am also adding my colleague’s Python module to the Python path.

Next, I will use the built-in “audiorecorder” function in MATLAB to listen to my microphone and display the resulting audio signal. Let’s listen for 5 seconds and give the program a sentence to test. For example, “I went to my favorite restaurant and had a delicious meal”.

Now that we have the audio data, it’s time to call the Python code. With this line of code, I am instructing MATLAB to call the “audioToText” function in the “mySpeechRecognizer” Python package. I can then extract the output data from the returned list of Python outputs.

We have the recognized text in MATLAB. Finally, I will call a MATLAB function to perform sentiment analysis. As you can see, the predicted sentiment is positive, which seems correct. If you’re wondering what’s in this function, I am using a built-in sentiment analysis algorithm called VADER from Text Analytics Toolbox.

That concludes our example. To summarize, let’s talk about why you would want to call Python from MATLAB. The workflow we showed fits if you are already working in MATLAB and want to use Python to solve part of the problem. This could be either that you have existing Python code you would like to reuse, or you need to access functionality that is only available in Python.

To learn more, check out the resources below. Also, make sure to watch our other video on how to call MATLAB from Python.

Related Products

  • MATLAB

Learn More

Download code
Using MATLAB with Python
System and configuration requirements to call Python from MATLAB
Call user-defined Python modules in MATLAB
Train a sentiment classifier
Sequence classification using deep learning

Bridging Wireless Communications Design and Testing with MATLAB

Read white paper

Cheat Sheets for Using MATLAB with Python

Get cheat sheets

Feedback

Featured Product

MATLAB

  • Request Trial
  • Get Pricing

Up Next:

2:09
How to Store a Series of Vectors from a for Loop

Related Videos:

18:43
Emotion Recognition of Call Center Conversations
39:07
MATLAB Meets MINDSTORMS: How to Control LEGO NXT Robots...
31:29
MATLAB: Tips and Tricks
3:05
Technical Computing with MATLAB, Part 2: Importing from...

View more related videos

MathWorks - Domain Selector

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

  • Switzerland (English)
  • Switzerland (Deutsch)
  • Switzerland (Français)
  • 中国 (简体中文)
  • 中国 (English)

You can also select a web site from the following list:

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
    • 简体中文Chinese
    • English
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

  • 영업 담당 문의
  • 평가판 신청

MathWorks

Accelerating the pace of engineering and science

MathWorks는 엔지니어와 과학자들을 위한 테크니컬 컴퓨팅 소프트웨어 분야의 선도적인 개발업체입니다.

활용 분야 …

제품 소개

  • MATLAB
  • Simulink
  • 학생용 소프트웨어
  • 하드웨어 지원
  • File Exchange

다운로드 및 구매

  • 다운로드
  • 평가판 신청
  • 영업 상담
  • 가격 및 라이선스
  • MathWorks 스토어

사용 방법

  • 문서
  • 튜토리얼
  • 예제
  • 비디오 및 웨비나
  • 교육

지원

  • 설치 도움말
  • MATLAB Answers
  • 컨설팅
  • 라이선스 센터
  • 지원 문의

회사 정보

  • 채용
  • 뉴스 룸
  • 사회적 미션
  • 고객 사례
  • 회사 정보
  • Select a Web Site United States
  • 신뢰 센터
  • 등록 상표
  • 정보 취급 방침
  • 불법 복제 방지
  • 애플리케이션 상태
  • 매스웍스코리아 유한회사
  • 주소: 서울시 강남구 삼성동 테헤란로 521 파르나스타워 14층
  • 전화번호: 02-6006-5100
  • 대표자 : 이종민
  • 사업자 등록번호 : 120-86-60062

© 1994-2022 The MathWorks, Inc.

  • Naver
  • Facebook
  • Twitter
  • YouTube
  • LinkedIn
  • RSS

대화에 참여하기