Passing path as input argument in matlab.engine

I'm using the following lines of code to run main.m (I:/xxx/simcode/xxx/main) with path as input argument, using matlabe engine
import matlab.engine
eng = matlab.engine.start_matlab()
eng.run("I:/xxx/simcode/xxx/main, ['I:/xxx/xxxx/xxx/task5'])
eng.quit()
But I get an error,
self._result = pythonengine.getFEvalResult(self._future,self._nargout, None, out=self._out, err=self._err)
matlab.engine.MatlabExecutionError: Too many input arguments.
Could someone please suggest how to fix this?
I would like to run the main.m from python by passing an input argument which is a path.

 Respuesta aceptada

Steven Lord
Steven Lord el 18 de Jun. de 2023
The run function in MATLAB accepts only one input, the name of a script file. Is your main.m a script file or a function file? If the former it cannot accept input arguments; that's one of the defining characteristics of a script! If it's a function file, I think you can run it directly with something like (untested)
import matlab.engine
eng = matlab.engine.start_matlab()
eng.main("['I:/xxx/xxxx/xxx/task5']")
eng.quit()
Or if you need to change directories before calling your function, call cd then call feval in your MATLAB Engine session.
Note that I haven't used this functionality directly, I'm just going by what I see in the documentation.

4 comentarios

Deepa Maheshvare
Deepa Maheshvare el 18 de Jun. de 2023
Editada: Deepa Maheshvare el 18 de Jun. de 2023
Thanks a lot.
I tried,
import matlab.engine
eng = matlab.engine.start_matlab()
eng.main("['I:/xxx/xxxx/xxx/task5']")
eng.quit()
and unfortunately, I get the following error.
File "C:\Users\xxx\anaconda3\envs\xxxx\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)
matlab.engine.MatlabExecutionError: Too many output arguments.
I tried,
r =
eng.main("['I:/xxx/xxxx/xxx/task5']")
and I still get the same error.
main.m is a function which takes input argument TASK_DIR (path to a excel file) and it doesn't return any value.
function main(TASK_DIR)
To change to the directory in which main.m is saved, I was using
os.chdir(SIMULATOR_DIR)
Now, I have changed to
eng = matlab.engine.start_matlab()
eng.cd("I:/xxx/xxxx/factory", nargout=0)
eng.main(["I:/xxx/xxxx/xxx/task5"])
eng.quit()
for some reason I get the same error as reported above ( Too many output arguments).
I am not sure if nargot=0 is passed in the right place.
Could you please have a look? And could you also show an small example on how to use
`feval ` or `eval`? Can we use `nargout` in feval/eval?
Try:
eng = matlab.engine.start_matlab()
eng.cd("I:/xxx/xxxx/factory", nargout=0)
eng.main(["I:/xxx/xxxx/xxx/task5"], nargout=0)
eng.quit()
Thanks so much!
I don't get the error now.
If I may ask one last question,
There is minor issue :
main.m is present in I:/xxx/xxxx/factory
and main.m accesses a method in class io_utils which is in I:/xxx/xxxx/utils.
When running main.m in gui, I include all the subfolders in I:/xxx/xxxx/ to path.
However, I am not sure how to do the same while running from python.
I get the following error, I think the path to io_utils class is not recognized.
File I:\xxx\xxxx\factory\main.m, line 16, in main
Unable to resolve the name 'io_utils.get_task'.
Could you please suggest how the subfolders can be added to path?
Thanks a lot
I think I should be using eng.addpath('/path/to/subfolder').

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Versión

R2022b

Preguntada:

el 17 de Jun. de 2023

Comentada:

el 18 de Jun. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by