bastibe / transplant

Transplant is an easy way of calling Matlab from Python
https://transplant.readthedocs.io
Other
110 stars 26 forks source link

When I use 'transplant.Matlab()' to start up Matlab session , how could I use python code to clase the Matlab session?Now I need to close the window manually. Thanks so much! #91

Open chaohcc opened 3 years ago

chaohcc commented 3 years ago

I use python code: matlab.close(); matlab.exit(); matlab.send_message('die'), it seems does't work. Thank so much!

bastibe commented 3 years ago

Can you describe your problem in a bit more detail and provide some example code so we can reproduce it?

chaohcc commented 3 years ago

Can you describe your problem in a bit more detail and provide some example code so we can reproduce it?

Thank you!
import transplant matlab = transplant.Matlab() res = matlab.Myfun(data,para) Y = res[0] A= res[1] the code above is run correct,and start a matlab session, and run my matlab function "Myfun(data,para)", and return the result of matlab to python. My question is how should I use code or command in python to close the matlab session? Now the matlab session can't close itself, I need close it's window manually, and if the matlab session didn't close, the python code after the matlab session will not run. In other words, the code, Y = res[0] A= res[1] would run after I close the matlab session manually. Sorry, my English is poor, hope I have made it clearly, thank you!

bastibe commented 3 years ago

If I understand you correctly, your Myfun does not return? That would be a problem in Myfun, not Transplant.

However, you say "I need to close its window manually", which implies that you are doing graphical things inside Myfun. This can be tricky within Transplant, as Transplant does not / can not drawnow every so often, which some Matlab code requires. You can try adding a few drawnow() to your Myfun and see if that helps.

chaohcc commented 3 years ago

If I understand you correctly, your Myfun does not return? That would be a problem in Myfun, not Transplant.

However, you say "I need to close its window manually", which implies that you are doing graphical things inside Myfun. This can be tricky within Transplant, as Transplant does not / can not drawnow every so often, which some Matlab code requires. You can try adding a few drawnow() to your Myfun and see if that helps.

Thank you! my problem is my python code start matlab successfuly and open a matlab window, run Myfun correctly. but python code not continus until the matlab window closed . seems the matlab.exit() methed is not working.

bastibe commented 3 years ago

What sort of Matlab window is being opened? Like a figure? What operating system are you running?

chaohcc commented 3 years ago

What sort of Matlab window is being opened? Like a figure? What operating system are you running? Thank you! here is the code I start matlab image

I run on Windows10. The opened Matlab window is like the following picture: image

Best wishes! Thank you!

bastibe commented 3 years ago

Is that Window interactive, i.e. can you type something in there and get a response? That shouldn't happen.

Could you debug into your transplant_master.py, and report back the value of process_arguments in line 568?

chaohcc commented 3 years ago

Is that Window interactive, i.e. can you type something in there and get a response? That shouldn't happen.

Could you debug into your transplant_master.py, and report back the value of process_arguments in line 568?

Thank you! Yes, I can type something in window and it will run and give a response. I debug transplant_master.py, the value of process_arguments : image Thank you!

bastibe commented 3 years ago

If there is a window, it should not be interactive. If it is interactive, transplant must have crashed. It's a sad fact of Matlab that some errors can't be caught. Do you see any error messages that might indicate a crash of transplant_remote?

chaohcc commented 3 years ago

If there is a window, it should not be interactive. If it is interactive, transplant must have crashed. It's a sad fact of Matlab that some errors can't be caught. Do you see any error messages that might indicate a crash of transplant_remote? Thank you! I haven't see any error messages that might indicate a crash of transplant_remote. Is there any way to see it or debug?

bastibe commented 3 years ago

The errors should show up either in your Python console, or in Matlab's window. Although since you seem to be running inside an IDE, perhaps the IDE is eating STDERR?

Perhaps you will see an error if you try running your program in a plain terminal instead of an IDE.

chaohcc commented 3 years ago

The errors should show up either in your Python console, or in Matlab's window. Although since you seem to be running inside an IDE, perhaps the IDE is eating STDERR?

Perhaps you will see an error if you try running your program in a plain terminal instead of an IDE. okey, thank you so much, I will have a try running my program in a plain terminal.

alien19 commented 1 year ago

Hello, I have the same issue here -I use the transplant module on windows to call Matlab functions in Python but can't get rid of the command window that pop up neither exit the Matlab session-, has anyone found a solution.

edit: eng.quit() does close the command window but raises "RuntimeError: Process died unexpectedly" eng.close() returns 1 but doesn't do anything eng.exit() requires me to write in the matlab command ">> exit()" too to close the session

Thanks in advance.

bastibe commented 1 year ago

It may be that matlab on Windows behaves differently from matlab on unices. Try inserting exit() into https://github.com/bastibe/transplant/blob/master/transplant/transplant_remote.m#L37 and see if that helps.