What causes MATLAB to execute pasted code automatically?
Show older comments
Sometimes I will copy a piece of code that someone has put up in answer to a question, then paste the code into MATLAB at the command prompt. Often I will need to make a change to the code before I hit return, such as fixing a typo. Usually this works fine, but sometimes the code executes as soon as I paste it, resulting in the expected error due to the typo. If the block of code is long I usually end up pasting it into the editor, making the change, then copying and pasting into the command window to execute the code.
This is annoying, so what causes this sometimes and not other times?
Accepted Answer
More Answers (2)
Jan
on 1 Feb 2011
I'm using a small function to clean the contents of the clipboard:
function cv % reminds me to Ctrl-V
Str = clipboard('paste');
Index = max(find(~isspace(Str)));
clipboard('copy', Str(1:Index));
Afterwards I can user Ctrl-V without triggering the execution. It might be more convenient to use one of Yair's tricks, the java.awt.Robot or FEX:textInject to write directly to the command prompt. But I use it too seldom to need this.
Oleg Komarov
on 1 Feb 2011
0 votes
A visual example:

In the first part, if you select down to the next line, you include a carriage return, and whenever you paste to the cmd windonw the code is executed.
The second part is the tedious end-of-line selection...which once pasted stays on the cmd window until you 'enter' it.
I desume from this example that Jiro is correct.
Oleg
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!