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

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.

Related

How to control the figure which appears in bayesopt function?

bayesopt draws figures like this:
How to access such figures in order to modify title or something? If I use gcf it is not guaranteed I get the correct figure because I could change focus to another figure window during execution.
Apparently bayesopt does not allow you to return a figure handle. So I suggest that on the line directly after your call to bayesopt you call h=gcf;, thus forcing your program to return the figure handle to h, which can then be modified at any desired time, even when moving to other figures.
results = bayesopt(fun,vars,Name,Value); % execute bayesian optimisation
h = gcf; % directly after optimisation grab a figure handle
Now you can modify properties in h, e.g. for the title you'd simply do
h.CurrentAxes.Title.String = 'Your plot title'
The reason this works is that MATLAB does not display figures until the full code has finished running. At least that's the case for my script, where I generate a figure, perform a few minutes of optimisation, then generate another figure. Both figures get displayed at the same time, i.e. when MATLAB has finished running the full program. There's thus no way you can click other figures when the code is running, since they are simply not there. If you happen to have older figures open (from other scripts), focus gets shifted to the latest created figure anyway, the moment it's created in the code (so not when it's displayed), thus you'd need to click a figure in the few milliseconds between the bayesopt call finished and the gcf call, which I'd say is so improbable that it's not worth considering, especially since it requires manual intervention.
As was pointed out in comments by Cris Luengo and Dev-iL, the figures are tagged and can thus be found using findobj:
h1 = findobj(0,'tag','bayesopt.MinObjective')

Prepare command in 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.

Is there a way to automatically suppress Matlab from printing big matrices in command window?

Is there an option in matlab or a plugin/app or a trick such that if you are in an interactive command session, every time it would print out a matrix way too big for a human to look through, it redacts the output to either a warning of how big the matrix is or a summary (only a few rows and columns) of the matrix?
There are many times where I want to examine a matrix in the command window, but I didn't realize how big it was, so I accidentally printed the whole thing out. Or some place inside a function I did not code myself, someone missed a semicolon and I handed it a big matrix, and it dumps the whole thing in my command window.
It make sense that in 99.99% of the time, people do not intend to print a million row matrix in their interactive command window, right? It completely spams their scroll buffer and removes all useful information that you had on screen before.
So it makes much more sense for matlab to automatically assume that the user in interactive sessions want to output a summary of a big matrix, instead of dumping the whole thing into the command window. There should at least be such an option in the settings.
One possibility is to overload the display function, which is called automatically when you enter an expression that is not terminated by ;. For example, if you put the following function in a folder called "#double" anywhere on your MATLAB path, the default display behavior will be overridden for double arrays (this is based on Mohsen Nosratinia's display.m for displaying matrix dimensions):
% #double/display.m
function display(v)
% DISPLAY Display a variable, limiting the number of elements shown.
name = inputname(1);
if isempty(name)
name = 'ans';
end
maxElementsShown = 500;
newlines = repmat('\n',1,~strcmp(get(0,'FormatSpacing'),'compact'));
if numel(v)>maxElementsShown,
warning('display:varTooLong','Data not displayed because of length.');
% OR show the first N=maxElementsShown elements
% builtin('disp', v(1:maxElementsShown));
elseif numel(v)>0,
fprintf([newlines '%s = \n' newlines], name);
builtin('disp', v);
end
end
For example,
>> xx=1:10
xx =
1 2 3 4 5 6 7 8 9 10
>> xx=1:1e4
Warning: Data not displayed because of length.
> In double.display at 17
EDIT: Updated to respect 'compact' and 'loose' output format preference.
EDIT 2: Prevent displaying an empty array. This makes whos and other commands avoid an unnecessary display.

Get figure handle from different mfile and plot in GUI

I am tasked to design a GUI and I need to use variables and plots from a different mfile which I created earlier. I'm pretty confident about getting variables from the processing mfile but I'm not sure how to get the plots/figures.
So basically my question is whether I can get() a figure from my mfile and then set() an axes to that figure inside my GUI.
Note: The reason I am doing this is because I want to keep the processing of data separate from the GUI mfile. I could just dump all the processing in the callback of my process button but that's not good. I would also appreciate good coding practices for my case since I have never worked with GUI's before (only scripting with PHP and MATLAB)
Note2 (rundown of what has to be done): In the GUI we basically are supposed to load 2 files, we then press the "process" button and then 4 plots have to appear. All the processing code already exists in a previously written mfile (by me).
Thanks! :)
I figured it out myself! What I did was use gcf to get the current figure like so: output.worldmap = gcf I then passed the object back like so: setappdata(0,'output',output) and grabbed it again inside my callback function like so: getappdata(0,'output') and used the following function to set the axes set(output.worldmap,'CurrentAxes',handles.axes_worldmap) I also made sure that the correct axis was set before I actually ran my mfile which does the processing with axes(handles.worldmap)

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')