shinyypig / matlab-in-vscode

A vscode extension for matlab.
MIT License
45 stars 4 forks source link

Bug related to `%` in printf commands (v0.4.5, Windows, Python backend) #15

Closed rq-Chen closed 1 year ago

rq-Chen commented 1 year ago

Hi shinnypig,

Thanks for creating this extension. It is very useful! I found a bug that I guess is related to the character % in some commands, which is also mentioned in #9. I am using the marketplace version (0.4.5) on Windows 11 with Python 3.10.9 backend (MATLAB 2022b). I got this from the terminal:

>>> fprintf('This is fine.\n');   
This is fine.
>>> fprintf(' %s \n');          
Error: Character vector is not terminated properly.

>>> fprintf(' %%');    
Error: Character vector is not terminated properly.

>>> fprintf(' %d ', 3); 
Error: Character vector is not terminated properly.

I guess the % in the commands was treated as comment, which caused the error. The error also occurs when using ctrl+enter or shift+enter to execute printf commands.

Could you take a look at this? Thank you very much!

shinyypig commented 1 year ago

Indeed, I had noticed this bug earlier. When the "%" sign appears, the MATLAB Python engine throws an error. Therefore, in #9, I decided to remove all the code after "%" as comments, but obviously, this approach was incorrect and has been reverted.

I'm sorry, this is not an issue with my plugin, and I don't have the capability to fix it. However, I recommend using disp to avoid such error occurrences.

rq-Chen commented 1 year ago

Hi, thanks for your quick response! I might be wrong, but I wonder whether you actually forgot to revert the change. In matlab_engine.py, you are still removing all inputs after "%", which leads to the error.

Besides, I tried to run the following code directly in my Python terminal and it seems to work, so I suspect that this is not a problem with the engine itself:

>>> import matlab.engine
>>> eng = matlab.engine.start_matlab()
>>> eng.eval('fprintf("%d\\n", 3)', nargout=0)                 
3
>>> eng.eval('fprintf("%d\\n", 3) % some comments', nargout=0)
3

Could you take a look? Thank you very much!

shinyypig commented 1 year ago

Good job!

The python code matlab_engine.py was from another contributor, I even didn't notice that there also exists code that removes the inputs after "%".

I have commented the related code, and published a prerelease 0.4.6. However, I am unable to test it for now. If possible, I would appreciate it if you could help me test it. Thanks!

Just click the Switch to Pre-Release Version button.

image
rq-Chen commented 1 year ago

I think the bug has been solved. Thanks!