Prepare command in MATLAB - matlab

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.

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.

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: conv()')
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.

Related

How can I run an octave command from the command line, and be able to have a window pop up? [duplicate]

I am trying to run a function in octave from the command line. The function is currently run like so:
octave --silent --persist --eval 'function(input arguments)'
function.m contains a plot command. When I invoke octave with the above command line parameters, the plot does show but octave enters into its interactive mode. My question is:
Is there any way to get octave to display the plot without entering the interactive mode when it is invoked from the command line?
Just use pause after your plotting functions
You can use:
waitfor(h)
at the end, which waits for you to close the figure.
AFAIK, the plot window is a child process of octave and therefor can only be displayed when octave is running. Even if you plot something from the "interactive" mode leave the plot open and close octave, the plot will also disappear.
What you could do is to plot to some output file like posted here:
f = figure
set(f, "visible", "off")
plot([1,2,3,4])
print("MyPNG.png", "-dpng")
You need to select a proper graphics toolkit:
available_graphics_toolkits
ans =
{
[1,1] = fltk
[1,2] = gnuplot
}
The default is fltk which cannot write to file without displaying the plot. However, if you select gnuplot it will be able to write to file without displaying it first. In your file start with:
graphics_toolkit gnuplot
The problem is that when you run from command line, when it ends, the plot windows disappear with it.
#! /usr/bin/octave -qf
f = figure;
set(f, "visible", "off")
t=0:0.001:5*pi;
plot(t, sin(5*t)), grid
print("MyPNG.png", "-dpng")
This saves output to MyPNG.png in the directory where it is run.
Then you might open it with a visualization program.
Another option is to add
pause
at the end of the program so it waits for user input to terminate, therefore to close the plot window.
Cheers :)
Also can try wait for key.
while (waitforbuttonpress ()==0)
pause(1)
end

Batch script for new instances of MATLAB

I am trying to write a batch script which can call and run a MATLAB script in the following manner:
matlab -r plotFunction(a,b); quit %here, a=1:10 and b=1:10
matlab -r plotFunction(a,b); quit %in 2nd instance a=11:20, b=11:20
matlab -r plotFunction(a,b); quit %in 3rd instance a=21:30, b=21:30
and so on.
That is, each time a new instance of MATLAB opens up, calls the function plotFunction which performs plotting a 100 times and then the program (MATLAB) quits. Subsequent to this, another instance of the program opens, performs plotting a 100 times again (corresponding to a=11:20 and b=11:20) and quits again. And so forth. How to put this in a loop?
The batch_job toolbox does this for you.
You can pass variables defined in the Windows command prompt to MATLAB line this:
set AMIN='1'
set BMIN='1'
set AMAX='10'
set BMAX='10'
matlab -r "disp(str2double(%AMIN%):str2double(%AMAX%)),disp(str2double(%BMIN%):str2double(%BMAX%)); input('press a key to quit'); quit"
Edit:
This can be improved like this,
set AMIN=1
set BMIN=1
set AMAX=10
set BMAX=10
set MATPROG=^
arange=(%AMIN%:%AMAX%),^
brange=(%BMIN%:%BMAX%),^
[x,y]=meshgrid(arange,brange),^
aplusb=x+y,^
plot3(x,y,aplusb),^
input('press a key to quit'),^
quit
matlab -r "%MATPROG%"
Note that ^ is the batch file line continuation character.
This change will make it easier to convert to loops in the batch file, although I don't understand why you don't create the loops in a MATLAB function and call that instead to keep the batch files as simple as possible.

matlab -nodesktop command history: cannot be saved

1 I run matlab without GUI, sometimes I would like to see commandhisoty by using commandhistory function, but I found that if I run matlab without GUI, the program cannot save any command history like the GUI matlab, are there some methods to solve this problem?
2 I tyied to use diary, but it seems that it can only save all commands and outputs. In addition, it seems that I should type diary every time when I open matlab, the default option is diary off. How to make it more convenient to use

How to intercept key strokes in Matlab while GUI is running

Do you know how to read keyboard strokes into Matlab while a Matlab gui is running? (I.e., without using the "input" function which sends a prompt to the command window and needs you to press return).
We would like to avoid using a mex function if possible.
You would first have to declare your figure by handle:
fig = figure;
then you can set properties (in quotes below) to activate functions that you've written to respond to user interactions (with the # signs):
set(fig,'KeyPressFcn',#keyDownListener)
set(fig, 'KeyReleaseFcn', #keyUpListener);
set(fig,'WindowButtonDownFcn', #mouseDownListener);
set(fig,'WindowButtonUpFcn', #mouseUpListener);
set(fig,'WindowButtonMotionFcn', #mouseMoveListener);
The above example is from shooter03.m a MATLAB space shooter, an excellent source (from the matlab file exchange) for many aspects of user object interaction in MATLAB:
http://www.mathworks.com/matlabcentral/fileexchange/31330-daves-matlab-shooter/content/shooter03/shooter03.m
If your GUI is based on a figure, you can use the figure property keypressfcn to define a callback function that handles keyboard inputs. See the matlab help for further descriptions: http://www.mathworks.de/help/techdoc/ref/figure_props.html#KeyPressFcn
Try:
hf = figure;
get(hf,'CurrentCharacter')

How to make output of Matlab m file to stay for a while

I have matlab m file which plots 3 graphs using subplot. Now i also have a UNIX script which invokes this script and passes the function name as parameter.
I have two problems:
I am getting the warning Type-ahead buffer Overflow
The plot remains only for a few seconds before disappearing. How can I keep plot active til l user clicks on cross button?
thanks!
It's hard to answer your first question without some code.
You can use the pause command to wait for the user to press any key.