1

Is there a way in MATLAB to prepare a command programmatically (ie. writing a command directly to the command prompt) so the user can execute it by pressing an enter?

I want to implement my own "Did you mean:" functionality, that is built into MATLAB already.

IKavanagh
  • 6,089
  • 11
  • 42
  • 47
tiborsimon
  • 91
  • 1
  • 7
  • 1
    I'm sorry I don't seem to understand your question. Do you want MATLAB to get ready to run a function / command, and you wait for the user to push ENTER, and the command runs? – rayryeng Sep 22 '15 at 07:22
  • Yes, this is the functionality I want to achieve. It's like typing the command for the user, and it is on the user if the typed command will be executed or not (deleted from the command prompt with BACKSPACE or with ESC). – tiborsimon Sep 22 '15 at 07:30
  • Just use `input` and input a string of `yes` or `no`. If `yes`, execute the command. If `no`, don't. – rayryeng Sep 22 '15 at 07:33
  • @tiborsimon, why? What do you want to achieve? – Stewie Griffin Sep 22 '15 at 08:12
  • I want to create a similar functionality like the built in "Did you mean:" help. – tiborsimon Sep 22 '15 at 08:51

2 Answers2

7

It can be done using Java from Matlab to programmatically generate key events, along the lines of this answer.

Let's say the command you want to "prepare" is dir. Then

commandwindow; %// make Matlab command window have focus
robot = java.awt.Robot; %/ Java Robot class
robot.keyPress (java.awt.event.KeyEvent.VK_D); %// key press event
robot.keyRelease (java.awt.event.KeyEvent.VK_D); %// key release event
robot.keyPress (java.awt.event.KeyEvent.VK_I);
robot.keyRelease (java.awt.event.KeyEvent.VK_I);
robot.keyPress (java.awt.event.KeyEvent.VK_R);
robot.keyRelease (java.awt.event.KeyEvent.VK_R);

will type dir onto the command window, exactly as if the user had written it. Then pressing Enter will run the command.

Community
  • 1
  • 1
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
4

The short answer is no. This can't be done as you want it to. What you are trying to do is to write text to MATLAB's stdin and let it remain there unprocessed. Essentially a modified form of piping.

The closest option available in MATLAB is Evaluate Selection where when you select text you can cause MATLAB to execute it in the command prompt. MATLAB puts this text exactly where you want it but it also immediately executes it. There does not seem to be a way to halt this or emulate its functionality programmatically.

Writing to stdin in MATLAB is not allowed as you can see from

>> fprintf(0, 'help conv')
Error using fprintf
Operation is not implemented for requested file identifier.

where 0 denotes stdin, 1 denotes stdout and 2 denotes stderr. Another naive attempt would be to use fopen()

>> fid = fopen(0, 'w');
Error using fopen
Invalid filename.

but alas this fails too. However, we can see from the first attempt that what you desire

is not implemented

The only option to get exactly what you want is that possibly with some MATLAB hackery the ability is there but I'm not aware of it or anybody who has even attempted it. EDIT: Luis Mendo has provided the MATLAB hackery solution I was talking about.


The closest you could get to what you want is to use hyperlinks to run MATLAB commands like

>> disp('Did you mean: <a href="matlab:conv(a,b)">conv()</a>')
Did you mean: conv()

ans =

     12

where conv() is a hyperlink and clicking on it will execute conv(a,b) where a = 3; and b = 4; in this example.

Community
  • 1
  • 1
IKavanagh
  • 6,089
  • 11
  • 42
  • 47
  • 2
    Thanks for the clear explanation IKavanagh. However, the hyperlink trick will do the job for me with limited functionality though. Thanks again! – tiborsimon Sep 22 '15 at 10:15