Why do I receive an "Invalid User of Operator Error" when calling function from command line?

17 views (last 30 days)
I have a code in matlab titled "original_boresight_verification.m" located in a directory "C:\Users\17037\Documents\Code\Matlab\resepi_boresighting". It inputs a file location (as a string), and outputs a single line of numbers (separated by commas) to the command window. It does not return any values.
I need to run this code from a python script and am using the subprocess command, which I have tested (that part "should" be working). When I run this in the command window:
I receive an "Invalid Use of Operator" error. I do not know why. When running the code in the Matlab command window:
I receive no errors, works perfectly fine. If it would help I can upload the code, although it is fairly lengthy so I will not unless it would be helpful.
My question: Why am I receiving an error in the python script calling the matlab function when the matlab function works perfectly fine?
Any help would be greatly appreciated. Thank you :)

Accepted Answer

Thomas Klein
Thomas Klein on 26 Aug 2021
I figured it out and am posting the answer in case anyone else has this problem:
I had an extra semicolon after original_boresight_verification. It was this:
subprocess.run(['matlab', '-batch', 'original_boresight_verification; C:\\Users\\17037\\Documents\\35844\\ROCK-537F25-2021-08-24-20-25-08\\clouds\\ppk_cloud_1.las'], capture_output=True, text=True, cwd=working_dir)
and now it is:
subprocess.run(['matlab', '-batch', 'original_boresight_verification C:\\Users\\17037\\Documents\\35844\\ROCK-537F25-2021-08-24-20-25-08\\clouds\\ppk_cloud_1.las'], capture_output=True, text=True, cwd=working_dir)
This was causing varargin to have multiple inputs whereas it should only have one. Not sure why this returned an "Invalid use of operator" error though.
If you are having a similar problem I would suggest the following steps to work through the problem (this is what I did):
  1. Check your function works in Matlab
  2. Check your function works in the command window (by itself not with python): use "cd 'path'" to navigate to the directory of your matlab file, then try
>matlab -batch "function_name argument_go_here"
Once this works go back and put it in python using the subprocess module (do not forget to import it), it should work now. :)

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!