How to convert phyton script to matlab script

Dear All,
I have phyton script as attached.
Anyone can help me to convert it into matlab script? Or it can call in matlab else..
Please help me.

 採用された回答

Pratyush
Pratyush 2023 年 7 月 26 日
編集済み: Pratyush 2023 年 7 月 26 日

0 投票

Hi Akmal,
I understand that you want a way to call your python scripts from MATLAB. This could be done using the "system" function.
Let say you have a script "test.py". To call it from MATLAB run the following code:
% Path to the Python script
pythonScript = 'test.py';
% Command to execute the Python script
command = ['python ', pythonScript];
% Call the Python script from MATLAB
system(command);
You can refer to the following documentation to call your python scripts from MATLAB: Call Python from MATLAB - MATLAB & Simulink - MathWorks India

4 件のコメント

mohd akmal masud
mohd akmal masud 2023 年 7 月 26 日
編集済み: mohd akmal masud 2023 年 7 月 26 日
Dear @Pratyush,
I was run the command you given, but got error:
>> pythonScript = 'ProposedNetwork.py'; % ProposedNetwork is my file name
>> command = ['python ', pythonScript];
>> system(command);
Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.
I open my setting, I dont know how to do?
Pratyush
Pratyush 2023 年 7 月 26 日
Hi Akmal,
This could be bacause you may not have added python to your environment variable.
Download and add python to your path. You can follow steps from this link: How to add Python to Windows PATH? - GeeksforGeeks
To ensure that python is added to the path, run this command in the MATLAB command prompt. This will show the version of python installed.
!python --version
After that you can proceed with the above steps.
mohd akmal masud
mohd akmal masud 2023 年 7 月 26 日
編集済み: mohd akmal masud 2023 年 7 月 26 日
Dear @Pratyush,
I was add, then suceesful.
But, the new error appear
>> % Path to the Python script
pythonScript = 'ProposedNetwork.py';
% Command to execute the Python script
command = ['python ', pythonScript];
% Call the Python script from MATLAB
system(command);
Traceback (most recent call last):
File "C:\Users\Akmal\Downloads\ProposedNetwork.py", line 1, in <module>
import torch.nn.init as init
ModuleNotFoundError: No module named 'torch'
How to download the module torch?
Dear @Pratyush, as attached is my phython script.
Pratyush
Pratyush 2023 年 7 月 26 日

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

その他の回答 (0 件)

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by