Interface Python with MATLAB and Create Python Packages with MATLAB

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone today we will see how  we can run python in matlab environment   and how we can run matlab in python environment  as well as if i have some algorithm in matlab   how we can create python packages and how we  can run python packages in python environment   so we can interface that so first thing  which you require you have to install the python so this is the link for that if you require  the link if you don't know you can write in   a comment i can provide you that link once it  is installed just keep the command here pyenv   once you give this command it shows this some  values this environment variable these are the   environment variables if it is showing like this  then it is perfect if you are getting everything   blank it means you have to set the environment  variable for the python manually for the matlab   so for that what we can do just simply open the  control panel from here in the control panel just   simply search system and here is the option it  edit the system environment variable and i will go   here in the environment variable and remember you  can see it as a user variable also system variable   i i will prefer that use the system variable  so just i'm going to select the path i have   to set the path for my python means the python  which i have installed what is the path of that   don't delete or don't modify anything just click  here edit and there is a option uh for new just   click on the new and what environment variable  you have to give here in my case it is this one   python27 so my python is installed here so let  me show you so you have to give the environment   variable you have to set the environment variable  for the python library so here i have this python   and this is the link just simply i can copy this  from here and i can paste it here that's all that   then you have to do okay that's all so once we  have done restart your matlab and just check   that environment variable is set or not so okay  it is set it's working perfectly now uh in python   there are different ways to create variables like  dictionary you can create tuples you can create   you can create lists also let's say if i have  to create a list in python means how we can   create uh that that kind of variables in python  okay so simply let's say i'm going to save as a   py dot list a blank list will be created here  and if i type a dot and if i press the tab   whatever functions are available in python for  the list it is showing in matlab as well this   is a good way to run the python in the matlab  because matlab environment is very enrich and   you can interface very easily and it will be  everything will be visible for you let's say   i want to append some values in this list let's  say i'm going to append 34 see that a has a 34   so as we know in list we can add character  text integer anything uh we can add it   it doesn't matter that what kind of classes  i have now let's say i'm going to append here   hi um i am using matlab so simply and if you see  if i type here a and this way we can create the   list here and for the indexing in matlab we  know that it doesn't start from the zero it   starts from one so this is what is that index  number one what is that index number two and   let's say if i want to open the list on the list  so simply here i can create let's say 34 56 78   67 just bracket and you see that i have the list  under list also so if i if i have to call that   list simply it is at position number three index  is three this is the way how we can create that   okay uh this is the way and same way if you  go in the help of python just simply type the   python click here and lots of helps will be open  like python libraries in matlab calling matlab   from python you will get step by step process  are given like in a single line if i have to   create the list how we can create the list so this  this options are available here right so this kind   of things are available here accessing python  modules from matlab and all these things right   so i'm going to show you that things how we can  do now uh let's say that if i have to run a python   function or any kind of function it can be just  i am going to show you a very simple function   i am just using the bracket here uh simply  function i have created that is for the odd   event the function the python function name  is that my font and if x is divisible by 2   is remainder sorry remainder it is  a reminder it is 0 then print it is an even number or print it is an odd  number so simply i am going to save this   i have saved on my desktop so i am  going to run this function in matlab   so what i will do here i have these functions  always remember save as dot pi and what is the   benefit of using this bracket which i has used  just now so it uh automatically it will take the   indent also so you can also use bracket to write  any kind of script for the matlab python java or c   plus plus so i have used the bracket from here  so you can install this bracket things which i   have done here so okay so this is the bracket  so just if i if i want that it will take the   indent automatically so what will i do just i will  create a new from here new file and i will save it   as dot pi extension so automatically for example  let's say that print is here if i enter you see   that automatically it is taking indent right okay  i'm just going to save and close all these things   okay now here i'm going to switch the function  which i have created i have pasted here in the   matlab directory uh matlab in matlab working  directory if i have to run this function pi dot   and the python file so that is my font cc right  my font cc okay and dot so i can have multiple   functions and i can choose any one of them so  right now i have only one function that is called   my func right this is the my function which i am  going to execute here so simply dot this is the   uh python script file name and this is the  function inside that disk file so let's say i'm   going to give value 5 and it's saying it is our  number so it is working perfectly right if i give   34 you see that it's showing it is even this way  we can run any kind of python functions in matlab   the steps already you know next i  am going to show you how we can run matlab functions in python or matlab in the  python so for there are some steps which we   have to follow uh first step which you have  to follow let me show you how how to do that   if you have windows same way you can use for other  operating systems also first of all we have to   go here for the windows so this is the matlab  root extern engines and then python so let me   open this program file and see where i have  installed the matlab simply i will go here program   files and from here i can search where i have  matlab here and this is a directory root directory   of matlab where i have installed i'm going extern  then extern then i have to go here in engine   and here i have the python i have to just copy  this and i have to open the cmd command prompt   i have to open it as an admin remember because we  have to set up something then only we will be able   to run that so cd and this and now i am in the  same directory the next step is once i am in the   matlab extern engines python then we have to  configure something we have to install this   one python setup.pi installed so simply i'm going  to run this and you see that everything is done   now we don't have to do anything just simply we  can open the python environment and we can we   and we can open the matlab engine and we  can execute any kind of function so for that   we have to open the python environment so  here this is the cmd again uh just simply   i will type python here now you see that i am  in a python environment the first step is we   have to import the matlab engine so this is the  function here which you see import matlab.engine   just simply am going to run here okay it is  imported now i am going to start that engine so   multiple matlab engine we can uh  start here okay so here you see um ng is equal to matlab.engine.start this is the  function which we use to start it okay just   i'm going here again switching here just going to  write it now uh you see it will take a little time   to start matlab in python once it is started we  can execute any matlab function here in the python   and from the latest version you  can use this jupyter also and   once you have you jupyter you can run your  matlab functions here also in repeater   okay just i'm going here this is started now  i have matlab engine name is enz and whatever   function i want to execute just simply i will type  here point zero sign files why i'm using point   zero here because as a default matlab functions  use double precision floating type variable so in   in python we have to define the floating type  floating type we have to use decimal right so   same way now uh let's say i want 10 of that simply  it will be executed good so far so good how we can   run so any kind of functions you can execute  here now the last one which i am going to show   you how we can create the python packages from  matlab functions and we can learn in the python   environment okay just simply i'm going to close  this and i'm going to switch in matlab environment   okay a very simple equation i have created  that is called function z dot add dot   x comma y z is equal to x plus y so any kind  of functions you can create it will be very   complicated as well so just to show you just i  have created very simple so once uh this function   is created uh i have to create one testbench also  to execute that functions okay this is my function   and this is my script file remember this  is a function file this is a script file   so once i have defined the x value y value then  here i am going to plug j is equal to add x plus y   let me run this and check if it is working or not  i am going to switch here perfect it is working   and i have the function and to execute that  function i have created one test pins also okay to   create a python package just i will go in matlab  environment and from here i will go in this apps   just below if i go and application deployment just  simply i can open a library compiler from there   one thing remember here to run python packages  uh in python we require a matlab compiler runtime   okay so matlab for example if you run java  program you require a java runtime same for the   matlab applications matlab libraries we require  a matlab compiler runtime so matlab compiler   runtime is free if you require the link from  where to download just write in a comment and   i can provide you that link so depends on  the matlab version right now i have 2020b   release matlab so i have installed this already  for that okay now the latest releases that is   r2021a which was uh released today on itself  okay so once we have installed that that's   all you have to do only one time you have to  install if you haven't installed then choose run   time include in the package so automatically  a package will be deployed in your app in the   package so i have already installed so once  you will install this uh it will be here so   here my labyrinth time is there's only one time  you have to install now what kind of libraries   we can create we can create c library c plus plus  excel add-ins also com libraries javalibrary.net   in our case we are we have to create the python  package and here you can provide your name author   name email you can provide the icons also but in  our case we don't require we you we we provide   this informations when we create a meta.exe file  okay so once we have done it's asking add the   exported function the functions the add function  which we have created here okay done now the test   bench we which we have created here we have to  prove it so here is the option we can create a new   simple test means this test band will be will be  used to execute in the python it will tell me that   how we are going to execute matlab functions okay  this is a support file you may say so i am going   to add existing sample file so the simple file  which i have created that is simple one light   if you don't remember let me show you here again  um here you see this is the simple one and this   is the function to execute that i have created  this dashboard that's all which we had to do   and just package and just save this i have  already created so i'm going to add this here okay uh it is still executing so it will create  a binary then it will create the python package   then it will save in a folder so three folders  will be saved i will let you know which folder   are you going to use to run in a python so this  is the way so it uh even uh whatever deep learning   machine learning models do you have you can  execute in matlab from the matlab also if you   have deep machine learning model you can create  packages and you can interface with the python   so it will take a little time to do this  task if you are doing the first time   okay if you have any query or any questions you  can write me in a chat box i will let you know   now you see that binaries are created and these  are the folders so remember three folders will   be created we are going to use this folder and  you see that this setup.pi is again here we have   to run this first then we can run the matlab  functions in python so if i go in the simple   what you have to do just copy this simple.pi and  what is this simple.pi let me open it and show you   just i'm going to open in a notepad you see  that this is the way how we are going to run in   python so if you want to change the value so you  can use this simple one dot pi in my case and you   can modify the values to execute that okay here  you see that size is also defined you can modify   and execute this pattern so this is the  test bench for that okay just i have to   copy this and i have to go back and i have to  paste it here that's all which i have to do   and here simply if i type the cmd this directory  will be automatically added in the cmd now you see   already same directory we have here so what  we have to do python then this setup.pi pi and install once this is installed you see  that configurations are done now we can run   this function so how to run this function simply  type the python and simple simple one dot api   and it will be executed so first time if i run  again it will take a little time to execute that   so this is the way how we can uh execute matlab  function so let me summarize that first of all   we saw how to set the environment variable of  python and you see that we got that value i want   to change this value just simply i will open this  simple.pi and uh from in any let's say i'm going   to open the notepad and just simply whatever value  is there let's say instead of 5 i'm going to use   15 or here 16 and this will be let's say 15 and  i'm going to save this and i can open this again   and i can execute and the value will be changed  okay let me summarize again we saw how to install   set the environment variable how to run my python  uh in matlab how we can learn matlab in the python   and further we saw that if i have matlab function  then how we can create uh python packages and   how we can run in the python environment right so  that's all later i i will show you if uh subscribe   the if you subscribe my video you will see more  advan if you subscribe my channel you will see   more advanced uh functions also some also i will  like to show how we can interface with java and   some other libraries also that's very easy to do  using the matlab so that's all for today thanks everyone
Info
Channel: Akhilesh Kumar
Views: 897
Rating: 5 out of 5
Keywords: Python with MATLAB, MATLAB with Python, Python packages with MATLAB, run MATLAB in Python, run Python in MATLAB, interface MATLAB with Python, Create Python Packages with MATLAB, MATLAB Compiler SDK, MATLAB vs Python, Python packages MATLAB, Python modules matlab
Id: QrQel0ZHTJc
Channel Id: undefined
Length: 17min 37sec (1057 seconds)
Published: Thu Mar 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.